{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Description:\n",
    "这个Jupyter用Pytorch实现NeuralMF模型，也是最终的模型， 是MLP和GMF模型的结合版本"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:10:56.029570Z",
     "start_time": "2020-10-21T08:10:54.683174Z"
    }
   },
   "outputs": [],
   "source": [
    "import datetime\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from collections import Counter\n",
    "import heapq\n",
    "\n",
    "import torch\n",
    "from torch.utils.data import DataLoader, Dataset, TensorDataset\n",
    "\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim\n",
    "\n",
    "#from torchkeras import summary, Model\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:10:57.948436Z",
     "start_time": "2020-10-21T08:10:57.940460Z"
    }
   },
   "outputs": [],
   "source": [
    "# 一些超参数设置\n",
    "topK = 10\n",
    "num_factors = 8\n",
    "num_negatives = 4\n",
    "batch_size = 64\n",
    "lr = 0.001"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:11:01.923798Z",
     "start_time": "2020-10-21T08:10:59.769564Z"
    }
   },
   "outputs": [],
   "source": [
    "# 数据在processed Data里面\n",
    "train = np.load('ProcessedData/train.npy', allow_pickle=True).tolist()\n",
    "testRatings = np.load('ProcessedData/testRatings.npy').tolist()\n",
    "testNegatives = np.load('ProcessedData/testNegatives.npy').tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:11:50.449952Z",
     "start_time": "2020-10-21T08:11:50.431999Z"
    }
   },
   "outputs": [],
   "source": [
    "num_users, num_items = train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:11:53.202585Z",
     "start_time": "2020-10-21T08:11:53.179647Z"
    }
   },
   "outputs": [],
   "source": [
    "# 制作数据   用户打过分的为正样本， 用户没打分的为负样本， 负样本这里采用的采样的方式\n",
    "def get_train_instances(train, num_negatives):\n",
    "    user_input, item_input, labels = [], [], []\n",
    "    num_items = train.shape[1]\n",
    "    for (u, i) in train.keys():  # train.keys()是打分的用户和商品       \n",
    "        # positive instance\n",
    "        user_input.append(u)\n",
    "        item_input.append(i)\n",
    "        labels.append(1)\n",
    "        \n",
    "        # negative instance\n",
    "        for t in range(num_negatives):\n",
    "            j = np.random.randint(num_items)\n",
    "            while (u, j) in train:\n",
    "                j = np.random.randint(num_items)\n",
    "            #print(u, j)\n",
    "            user_input.append(u)\n",
    "            item_input.append(j)\n",
    "            labels.append(0)\n",
    "    return user_input, item_input, labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:12:05.764972Z",
     "start_time": "2020-10-21T08:11:54.169000Z"
    }
   },
   "outputs": [],
   "source": [
    "user_input, item_input, labels = get_train_instances(train, num_negatives)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:12:11.103685Z",
     "start_time": "2020-10-21T08:12:09.807155Z"
    }
   },
   "outputs": [],
   "source": [
    "train_x = np.vstack([user_input, item_input]).T\n",
    "labels = np.array(labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:12:14.605315Z",
     "start_time": "2020-10-21T08:12:14.527524Z"
    }
   },
   "outputs": [],
   "source": [
    "# 构建成Dataset和DataLoader\n",
    "train_dataset = TensorDataset(torch.tensor(train_x), torch.tensor(labels).float())\n",
    "dl_train = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T03:02:04.699632Z",
     "start_time": "2020-10-21T03:02:04.188999Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[2302, 1126],\n",
      "        [3674, 1319],\n",
      "        [1327, 1503],\n",
      "        [4380,  645],\n",
      "        [4222, 1845],\n",
      "        [1692, 2762],\n",
      "        [5353,  493],\n",
      "        [2528, 2027],\n",
      "        [3820, 1421],\n",
      "        [1940, 3362],\n",
      "        [5223, 3171],\n",
      "        [2184, 3551],\n",
      "        [3393, 1125],\n",
      "        [3131, 2055],\n",
      "        [4132, 2661],\n",
      "        [4851, 1986],\n",
      "        [1120, 1042],\n",
      "        [5479,  330],\n",
      "        [3767, 1155],\n",
      "        [6023,  218],\n",
      "        [3415,  349],\n",
      "        [2994, 1069],\n",
      "        [4778,  178],\n",
      "        [2665, 3239],\n",
      "        [2053,  660],\n",
      "        [1180, 3045],\n",
      "        [4660, 3168],\n",
      "        [5629,  328],\n",
      "        [ 966, 2675],\n",
      "        [ 300, 1341],\n",
      "        [2228, 3028],\n",
      "        [4226, 1127],\n",
      "        [ 373, 2602],\n",
      "        [5685, 2586],\n",
      "        [4417,  552],\n",
      "        [5794, 2885],\n",
      "        [ 763, 1347],\n",
      "        [ 684, 2559],\n",
      "        [1263, 2891],\n",
      "        [2873, 3418],\n",
      "        [1570,  316],\n",
      "        [1638, 2306],\n",
      "        [3761, 1739],\n",
      "        [ 427, 1194],\n",
      "        [1921, 2472],\n",
      "        [2518,  735],\n",
      "        [4960,  961],\n",
      "        [4410,  389],\n",
      "        [1077,  926],\n",
      "        [1911, 3526],\n",
      "        [4481, 1387],\n",
      "        [5322,  586],\n",
      "        [ 180,  373],\n",
      "        [4521,  755],\n",
      "        [4853, 2300],\n",
      "        [4516, 2686],\n",
      "        [3926,  110],\n",
      "        [2161,  413],\n",
      "        [4577, 2285],\n",
      "        [5613,  923],\n",
      "        [3212,   76],\n",
      "        [1725, 2203],\n",
      "        [5839, 1522],\n",
      "        [ 962,  706]], dtype=torch.int32) tensor([1., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
      "        1., 1., 0., 1., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 1., 0.,\n",
      "        1., 0., 0., 0., 1., 0., 1., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 1.,\n",
      "        0., 0., 1., 0., 0., 0., 0., 0., 0., 0.])\n"
     ]
    }
   ],
   "source": [
    "# 测试一下\n",
    "for (x, y) in iter(dl_train):\n",
    "    print(x, y)\n",
    "    break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NeuralMF模型\n",
    "这里建立NeuralMF模型， 这个模型是MLP和GMF的合并版本， 图片如下：\n",
    "\n",
    "![](img/1.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:13:33.493224Z",
     "start_time": "2020-10-21T08:13:33.464303Z"
    }
   },
   "outputs": [],
   "source": [
    "class NeuralMF(nn.Module):\n",
    "    \n",
    "    def __init__(self, num_users, num_items, mf_dim, layers):\n",
    "        super(NeuralMF, self).__init__()\n",
    "        \n",
    "        self.MF_Embedding_User = nn.Embedding(num_embeddings=num_users, embedding_dim=mf_dim)\n",
    "        self.MF_Embedding_Item = nn.Embedding(num_embeddings=num_items, embedding_dim=mf_dim)\n",
    "        \n",
    "        self.MLP_Embedding_User = nn.Embedding(num_embeddings=num_users, embedding_dim=layers[0] // 2)\n",
    "        self.MLP_Embedding_Item = nn.Embedding(num_embeddings=num_items, embedding_dim=layers[0] // 2)\n",
    "        \n",
    "        # 全连接网络\n",
    "        self.dnn_network = nn.ModuleList([nn.Linear(layer[0], layer[1]) for layer in list(zip(layers[:-1], layers[1:]))])\n",
    "        self.linear = nn.Linear(layers[-1], mf_dim)\n",
    "        \n",
    "        # 合并之后\n",
    "        self.linear2 = nn.Linear(2*mf_dim, 1)\n",
    "        self.sigmoid = nn.Sigmoid()\n",
    "    \n",
    "    def forward(self, inputs):\n",
    "        # 这个inputs是一个批次的数据， 所以后面的操作切记写成inputs[0], [1]这种， 这是针对某个样本了， 我们都是对列进行的操作\n",
    "        \n",
    "        # 先把输入转成long类型\n",
    "        inputs = inputs.long()\n",
    "        \n",
    "        # MF模型的计算 用户和物品的embedding\n",
    "        MF_Embedding_User = self.MF_Embedding_User(inputs[:, 0])  # 这里踩了个坑， 千万不要写成[0]， 我们这里是第一列\n",
    "        MF_Embedding_Item = self.MF_Embedding_Item(inputs[:, 1])\n",
    "        # 两个向量点积过一个全连接\n",
    "        mf_vec = torch.mul(MF_Embedding_User, MF_Embedding_Item)\n",
    "        \n",
    "        # MLP 模型的计算\n",
    "        MLP_Embedding_User = self.MLP_Embedding_User(inputs[:, 0])  \n",
    "        MLP_Embedding_Item = self.MLP_Embedding_Item(inputs[:, 1])\n",
    "        # 两个隐向量堆叠起来\n",
    "        x = torch.cat([MF_Embedding_User, MF_Embedding_Item], dim=-1)\n",
    "        # l全连接网络\n",
    "        for linear in self.dnn_network:\n",
    "            x = linear(x)\n",
    "            x = F.relu(x)\n",
    "        mlp_vec = self.linear(x)\n",
    "        \n",
    "        # 合并两个\n",
    "        vector = torch.cat([mf_vec, mlp_vec], dim=-1)\n",
    "        \n",
    "        # liner\n",
    "        linear = self.linear2(vector)\n",
    "        output = self.sigmoid(linear)\n",
    "        \n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:13:37.320982Z",
     "start_time": "2020-10-21T08:13:37.301037Z"
    }
   },
   "outputs": [],
   "source": [
    "# 看一下这个网络\n",
    "model = NeuralMF(1, 1, 10, [20, 64, 32, 16])\n",
    "#summary(model, input_shape=(2,))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 建立模型 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:14:24.328200Z",
     "start_time": "2020-10-21T08:14:21.633410Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NeuralMF(\n",
       "  (MF_Embedding_User): Embedding(6040, 8)\n",
       "  (MF_Embedding_Item): Embedding(3706, 8)\n",
       "  (MLP_Embedding_User): Embedding(6040, 8)\n",
       "  (MLP_Embedding_Item): Embedding(3706, 8)\n",
       "  (dnn_network): ModuleList(\n",
       "    (0): Linear(in_features=16, out_features=64, bias=True)\n",
       "    (1): Linear(in_features=64, out_features=32, bias=True)\n",
       "    (2): Linear(in_features=32, out_features=16, bias=True)\n",
       "  )\n",
       "  (linear): Linear(in_features=16, out_features=8, bias=True)\n",
       "  (linear2): Linear(in_features=16, out_features=1, bias=True)\n",
       "  (sigmoid): Sigmoid()\n",
       ")"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 设置\n",
    "layers = [num_factors*2, 64, 32, 16]\n",
    "model = NeuralMF(num_users, num_items, num_factors, layers)\n",
    "model.to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:21:17.812764Z",
     "start_time": "2020-10-21T08:21:17.799799Z"
    }
   },
   "outputs": [],
   "source": [
    "children_dict = {name:module for name,module in model.named_children()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:24:15.822942Z",
     "start_time": "2020-10-21T08:24:15.815915Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['MF_Embedding_User.weight', 'MF_Embedding_Item.weight', 'MLP_Embedding_User.weight', 'MLP_Embedding_Item.weight', 'dnn_network.0.weight', 'dnn_network.0.bias', 'dnn_network.1.weight', 'dnn_network.1.bias', 'dnn_network.2.weight', 'dnn_network.2.bias', 'linear.weight', 'linear.bias', 'linear2.weight', 'linear2.bias'])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict(model.state_dict()).keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:35:59.560532Z",
     "start_time": "2020-10-21T08:35:59.397283Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter containing:\n",
      "tensor([[ 1.1709, -0.4426, -0.6260,  ...,  1.0484, -0.8389,  1.8611],\n",
      "        [ 0.1521,  0.6039, -0.3019,  ...,  0.8437, -0.6239, -0.9326],\n",
      "        [ 1.7790, -1.1589, -0.2400,  ...,  0.4139, -0.5713, -1.6345],\n",
      "        ...,\n",
      "        [-0.3822, -0.2985, -0.0359,  ...,  0.9410, -0.4607,  0.4453],\n",
      "        [-1.0245,  1.3295, -0.3865,  ..., -0.5181,  0.3817, -0.0125],\n",
      "        [-1.2858, -0.3548, -0.3710,  ...,  2.1751, -1.6506, -1.8563]],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([[-0.2087, -0.6825,  1.4534,  ..., -0.0390,  0.6873, -0.0701],\n",
      "        [ 0.6477, -0.1886, -2.0421,  ...,  0.7109, -2.4402,  1.6883],\n",
      "        [-0.6667, -0.7265, -0.0233,  ..., -0.0959, -0.0890, -0.7599],\n",
      "        ...,\n",
      "        [-0.6539,  0.4124, -0.2805,  ...,  1.1740, -0.7151,  0.5618],\n",
      "        [-1.0864,  0.4361,  0.2328,  ..., -0.3536, -1.2812,  0.2452],\n",
      "        [-1.7298,  0.2811,  0.0380,  ..., -0.8430, -2.0017,  0.9016]],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([[-0.1593, -0.1449,  0.8657,  ..., -0.0278,  2.1225,  1.0159],\n",
      "        [-0.5894, -1.3302,  0.0648,  ..., -0.1840,  0.6809,  0.1808],\n",
      "        [ 0.1036, -0.2784, -0.0101,  ...,  0.9558, -1.9841, -1.0930],\n",
      "        ...,\n",
      "        [-0.4526,  1.3361,  0.6675,  ...,  0.4798, -1.2746, -0.7504],\n",
      "        [ 0.0110,  0.0236, -0.5544,  ..., -1.2799, -0.5241,  1.4705],\n",
      "        [-0.3426, -0.6744, -0.9088,  ...,  0.0034, -0.1481,  1.0776]],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([[-1.5621,  1.3409, -0.5320,  ...,  0.4549,  0.3982,  0.3804],\n",
      "        [-0.0458,  0.3475,  0.3725,  ...,  0.2308, -0.8268,  0.1960],\n",
      "        [-1.6346,  1.3065,  0.9099,  ...,  0.4356, -0.2991,  1.7853],\n",
      "        ...,\n",
      "        [-0.6027,  1.0905,  0.5676,  ...,  0.2901,  0.1857,  0.0303],\n",
      "        [ 1.0395, -0.4323,  1.6369,  ..., -0.2480,  0.6916, -0.3178],\n",
      "        [-0.7154, -1.2933,  1.8044,  ...,  0.2396, -1.0494, -1.5359]],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([[-0.1057, -0.0388, -0.0746,  ..., -0.1448,  0.1393, -0.0003],\n",
      "        [ 0.1579, -0.2246,  0.1264,  ...,  0.1867, -0.1587, -0.1196],\n",
      "        [ 0.0904, -0.2471,  0.0826,  ..., -0.0081, -0.1484, -0.0424],\n",
      "        ...,\n",
      "        [-0.0923,  0.2424,  0.1392,  ..., -0.1391, -0.1320,  0.1116],\n",
      "        [ 0.0686,  0.2356,  0.1453,  ...,  0.0599, -0.1995, -0.1521],\n",
      "        [ 0.2425, -0.1023,  0.2172,  ..., -0.2199, -0.0115, -0.1120]],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([ 0.0038, -0.1085,  0.1284, -0.1983,  0.1435,  0.0268,  0.1757,  0.1017,\n",
      "         0.1401, -0.1192, -0.1621,  0.0723,  0.2397,  0.0553, -0.0177, -0.0771,\n",
      "        -0.0329, -0.0603,  0.1355,  0.2398,  0.0706, -0.2467, -0.1598,  0.2144,\n",
      "         0.0399,  0.2225, -0.0086,  0.2349, -0.1852, -0.1646, -0.1758, -0.1610,\n",
      "        -0.1249, -0.1292,  0.0678,  0.2292, -0.2330,  0.2444, -0.0048, -0.0244,\n",
      "        -0.1525,  0.1185, -0.0568, -0.0617, -0.0527, -0.1155,  0.0987,  0.0437,\n",
      "        -0.1994, -0.0502,  0.2454,  0.1201,  0.1287, -0.0666,  0.0118,  0.1672,\n",
      "         0.0171,  0.0089,  0.1110, -0.2437,  0.1418, -0.0560,  0.0944,  0.0230],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([[ 0.0790,  0.0320, -0.1072,  ..., -0.0400,  0.0298, -0.0115],\n",
      "        [-0.0891,  0.1118, -0.0537,  ..., -0.0924,  0.1124, -0.0893],\n",
      "        [-0.0852, -0.0615, -0.0583,  ...,  0.0449, -0.0493,  0.0987],\n",
      "        ...,\n",
      "        [-0.0020,  0.0177, -0.0980,  ..., -0.0388, -0.0901,  0.1159],\n",
      "        [-0.0603, -0.1239,  0.0087,  ...,  0.0851,  0.0384, -0.0689],\n",
      "        [-0.0143, -0.0546,  0.0771,  ..., -0.0021, -0.0637, -0.1158]],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([-0.0671, -0.0008, -0.1095,  0.0378,  0.1143, -0.0364,  0.1118,  0.0566,\n",
      "        -0.0762, -0.1009,  0.0684, -0.0861,  0.1221, -0.0509,  0.0332, -0.0795,\n",
      "         0.0305,  0.0778,  0.0754, -0.0135,  0.0121,  0.0998,  0.0296, -0.0430,\n",
      "         0.0510, -0.0505,  0.0759,  0.0734,  0.1193,  0.0321, -0.0221, -0.0082],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([[ 1.3231e-01,  1.7161e-01, -7.9663e-02, -6.6087e-02,  7.2313e-02,\n",
      "         -7.0428e-03, -7.4116e-02, -2.6709e-02,  1.1669e-01, -1.0944e-01,\n",
      "         -1.1492e-01,  2.7449e-02, -3.5873e-02, -2.6502e-02, -6.2235e-02,\n",
      "         -1.1510e-01,  8.2103e-02,  1.1808e-01, -7.6338e-02,  4.3379e-02,\n",
      "          1.6913e-02, -1.0221e-01,  2.7811e-02,  9.1617e-02, -8.8658e-02,\n",
      "          1.2108e-01,  2.7562e-02, -6.3343e-02, -1.6652e-01, -1.4026e-01,\n",
      "          6.3965e-02,  5.6991e-02],\n",
      "        [ 3.1751e-02, -5.1747e-03,  5.2829e-02,  8.4061e-02,  1.3461e-01,\n",
      "         -7.1605e-02,  3.2991e-03, -3.6656e-02,  1.0121e-01, -3.8389e-02,\n",
      "          2.3480e-02,  1.6325e-01, -1.2225e-01,  9.2435e-02, -1.5716e-01,\n",
      "          9.4118e-02, -1.6460e-01,  1.2658e-01, -9.4958e-02, -1.5604e-01,\n",
      "         -8.9144e-02,  4.6893e-02, -1.3332e-01, -1.7285e-01,  1.7101e-01,\n",
      "         -4.2875e-02, -7.6275e-02, -1.4873e-01, -3.5864e-02,  1.0927e-02,\n",
      "          1.4001e-01, -5.4145e-02],\n",
      "        [-4.4268e-02, -9.7129e-02, -1.9770e-02, -1.5762e-01, -1.7266e-01,\n",
      "         -9.1746e-02, -4.6801e-02, -7.9677e-02,  9.9758e-02, -8.6668e-03,\n",
      "         -2.3177e-02, -3.0766e-02,  1.4322e-01,  9.0108e-02, -1.3843e-01,\n",
      "         -1.3529e-01, -1.3661e-01,  4.3179e-02, -6.7406e-02,  3.3858e-02,\n",
      "          3.4062e-03, -1.0068e-01, -1.1028e-01,  1.4961e-01, -1.5131e-01,\n",
      "         -9.4342e-02,  8.0164e-02, -1.1863e-01, -1.0712e-01, -7.8745e-02,\n",
      "         -2.8055e-02,  1.6194e-01],\n",
      "        [ 1.5637e-01,  1.9127e-02, -8.6468e-02, -5.9061e-02,  8.3907e-02,\n",
      "          1.3937e-01, -6.1777e-02, -8.4981e-02,  4.2080e-03,  1.2555e-01,\n",
      "         -8.0573e-02,  1.2349e-01, -1.0495e-01,  5.5012e-02, -7.6524e-02,\n",
      "          1.2571e-01, -7.9852e-03, -1.1693e-01,  8.3001e-03,  1.3135e-01,\n",
      "         -1.1177e-01, -7.9212e-02, -1.7606e-01,  4.8658e-02, -3.2037e-02,\n",
      "          8.9781e-02,  2.7523e-02, -1.2653e-01,  1.7572e-01, -3.6468e-02,\n",
      "         -1.5139e-01, -5.8740e-02],\n",
      "        [-8.2764e-02, -4.4539e-02, -1.2996e-01, -2.4912e-02, -1.6007e-01,\n",
      "          9.3651e-02,  1.2019e-02,  2.8511e-02, -4.1490e-02,  1.6036e-01,\n",
      "          4.4922e-02,  4.3243e-02, -6.7430e-02, -1.6285e-01,  4.3508e-02,\n",
      "          1.2727e-01,  1.5711e-01, -5.8470e-03, -1.4502e-01,  1.3712e-02,\n",
      "          1.1607e-01,  2.1350e-02, -4.9893e-02, -1.5941e-01, -1.0843e-01,\n",
      "         -1.4598e-01, -1.5919e-01, -1.3314e-01,  1.6475e-02,  9.7974e-02,\n",
      "         -7.8873e-02,  1.3469e-01],\n",
      "        [-1.0164e-01, -1.3167e-01, -9.2086e-03,  3.7663e-03, -1.7358e-01,\n",
      "          1.6340e-01, -4.5292e-02,  1.3332e-01,  8.2941e-02,  1.5821e-01,\n",
      "          1.1835e-01,  2.9279e-02,  1.2498e-01,  9.1091e-02, -4.4916e-02,\n",
      "          1.0504e-01,  1.1075e-01, -8.1874e-02,  5.0558e-02, -1.4584e-01,\n",
      "         -1.4702e-01,  1.0164e-01,  1.5720e-01, -3.9976e-03,  1.7246e-01,\n",
      "         -5.0796e-02, -1.4048e-01, -4.5570e-02, -8.5296e-02, -1.6655e-01,\n",
      "         -9.9070e-02, -3.9908e-02],\n",
      "        [ 2.7165e-02, -1.2648e-01, -7.0947e-02,  4.2860e-02,  1.1822e-01,\n",
      "         -1.3340e-01,  1.0475e-03, -7.6958e-02, -1.2169e-01, -1.6696e-01,\n",
      "          1.4581e-01,  1.0088e-02,  1.4191e-02,  7.8756e-02,  4.7051e-02,\n",
      "         -1.5234e-01, -9.3089e-02,  5.7264e-03, -5.6962e-02, -1.5986e-01,\n",
      "         -4.7170e-02,  1.4183e-01, -1.2807e-01, -2.4815e-02, -2.5292e-02,\n",
      "         -1.0678e-01,  1.3632e-01, -1.5603e-02,  1.2350e-01,  3.2988e-02,\n",
      "          8.8043e-03,  5.5091e-02],\n",
      "        [-1.2463e-01, -2.6941e-02,  1.7469e-01, -6.9250e-02, -1.7121e-01,\n",
      "         -1.2037e-01,  5.8275e-02,  1.8675e-02, -2.0104e-02,  1.0964e-01,\n",
      "          6.9669e-02,  1.4190e-01, -1.6691e-01, -9.3985e-02,  5.7810e-02,\n",
      "         -8.1785e-02, -1.4629e-01, -5.1731e-02, -1.5432e-01, -1.6733e-01,\n",
      "          9.4869e-02,  4.4022e-02,  4.1353e-02, -1.7407e-01,  3.0786e-02,\n",
      "         -1.4414e-01, -8.1216e-02, -8.4872e-02, -1.3910e-01, -1.4063e-01,\n",
      "         -1.6462e-01,  1.6598e-01],\n",
      "        [-7.8842e-03,  1.2665e-01,  3.7027e-02, -1.4323e-01,  3.9726e-02,\n",
      "          6.9125e-02,  7.0102e-02,  1.6648e-01,  2.1966e-02, -8.1430e-03,\n",
      "         -9.1661e-02,  6.4401e-02, -1.9589e-02,  6.7400e-02, -1.4929e-01,\n",
      "         -6.2151e-02, -8.1230e-02, -2.6271e-02,  1.4541e-01,  1.2889e-01,\n",
      "          1.1838e-01, -1.6365e-01, -1.4346e-02,  1.0000e-02,  4.7192e-02,\n",
      "          1.6549e-01, -9.3731e-03, -9.8193e-02, -2.7962e-02, -2.8830e-03,\n",
      "         -8.5624e-02,  1.0671e-01],\n",
      "        [-1.3021e-01, -5.8480e-02, -2.0659e-02,  7.4067e-02, -1.5627e-01,\n",
      "          5.4821e-02,  1.1364e-01, -9.3193e-02,  7.9265e-02, -1.0235e-01,\n",
      "         -6.7463e-02, -7.8114e-02, -3.0247e-02, -6.4865e-02,  9.4094e-02,\n",
      "          8.8420e-02,  1.0354e-01,  1.1676e-01,  1.4087e-01,  8.7614e-02,\n",
      "          3.7042e-02,  4.7139e-02,  4.0541e-02,  1.5832e-01, -1.3623e-01,\n",
      "          2.6773e-02,  4.9648e-02,  7.7690e-02, -1.7393e-01,  2.2752e-02,\n",
      "          9.9097e-02,  5.9978e-02],\n",
      "        [ 2.2733e-02, -7.7989e-03, -3.4532e-02, -2.0373e-02,  1.8727e-02,\n",
      "         -1.0328e-02,  1.6916e-01, -1.6738e-01, -1.5847e-01,  5.7684e-02,\n",
      "          9.9092e-02,  1.0065e-01, -1.7252e-01,  7.2526e-02, -5.3994e-02,\n",
      "         -2.2663e-02, -1.7537e-01,  5.6699e-02,  1.0820e-01, -1.4409e-03,\n",
      "          3.7309e-02,  1.5746e-01,  5.0941e-02, -4.8597e-02,  7.0109e-02,\n",
      "         -1.3943e-01,  8.8900e-02, -2.4203e-02,  1.4770e-01,  6.3355e-02,\n",
      "         -1.7470e-02,  1.1994e-01],\n",
      "        [ 7.7691e-02,  8.3963e-02, -1.2002e-01,  1.2892e-01, -1.5212e-01,\n",
      "         -1.1292e-01, -1.6704e-01,  1.5200e-02, -3.6921e-02, -4.9396e-02,\n",
      "          1.5125e-01,  1.7854e-03,  5.3928e-02, -5.5673e-02,  1.3288e-01,\n",
      "          1.1415e-01, -6.2858e-02, -1.0946e-01,  1.7384e-01,  1.5887e-01,\n",
      "          1.4039e-01,  2.9397e-02, -1.4008e-01,  1.3613e-01,  5.8514e-02,\n",
      "          1.2053e-01,  1.0748e-01, -1.9062e-02,  1.3223e-01, -4.8418e-02,\n",
      "          1.2351e-01, -7.1749e-05],\n",
      "        [-5.4029e-03,  3.0487e-02, -2.4916e-02, -1.1601e-01,  1.6329e-02,\n",
      "         -1.6791e-01,  1.5793e-01,  1.5376e-01,  1.3766e-01, -1.4268e-01,\n",
      "          1.1087e-01,  9.4531e-02,  7.5085e-02,  1.4988e-01,  1.1820e-01,\n",
      "          1.7183e-01, -1.0092e-01, -1.3785e-01,  2.4446e-02,  1.6434e-03,\n",
      "          1.6844e-01, -1.4247e-01, -4.8005e-02, -1.3456e-01,  1.4413e-01,\n",
      "          1.2327e-01,  9.6344e-02, -1.1365e-01,  4.7185e-02, -8.2437e-02,\n",
      "         -8.4922e-02, -1.5262e-01],\n",
      "        [ 1.0566e-02, -1.5965e-01, -1.3199e-01,  1.6686e-01, -3.2197e-02,\n",
      "         -1.6447e-01,  3.5896e-02, -1.4321e-01, -6.1707e-02, -3.8745e-02,\n",
      "         -8.9757e-03, -9.2084e-02,  1.2514e-01, -1.3568e-01,  1.5623e-01,\n",
      "          1.5002e-01,  8.0180e-04, -1.3328e-01,  9.8998e-02, -1.4628e-02,\n",
      "          2.0349e-02, -3.9385e-02,  1.2775e-01, -1.4253e-01, -3.2779e-02,\n",
      "          1.5787e-01,  5.6599e-02,  1.6346e-01,  1.0240e-01,  5.1866e-02,\n",
      "         -8.0191e-02,  8.8055e-02],\n",
      "        [-1.5651e-01,  1.6412e-01, -1.6607e-01,  8.5786e-02,  7.6553e-03,\n",
      "          6.4794e-02,  6.3712e-02,  1.4374e-01,  8.3151e-02, -1.4415e-01,\n",
      "         -1.5379e-01,  6.9623e-02, -1.3235e-01, -6.2226e-03,  3.2460e-02,\n",
      "         -1.2776e-01,  1.7557e-01, -4.8863e-02, -1.5013e-01,  2.3789e-02,\n",
      "          3.5149e-02, -8.3692e-02,  7.1438e-02, -1.4133e-01,  3.6860e-02,\n",
      "         -5.9530e-02, -4.0767e-03, -1.3581e-01,  7.3144e-04,  1.0802e-01,\n",
      "          1.5293e-01, -6.3260e-02],\n",
      "        [ 1.0380e-01,  1.0795e-01,  1.8010e-03, -8.8283e-02,  1.0498e-02,\n",
      "          1.7385e-01,  5.7879e-03,  1.4100e-01,  1.1745e-01,  1.5792e-01,\n",
      "          1.7401e-01,  1.6369e-01,  6.9170e-02,  1.6860e-01,  4.7596e-02,\n",
      "          9.9478e-02, -1.3795e-01,  9.1257e-02,  1.1923e-01, -1.5582e-01,\n",
      "         -1.2846e-01, -1.3412e-01,  1.0605e-01,  1.1437e-01,  1.9436e-02,\n",
      "         -1.4764e-01, -1.4400e-01,  1.3903e-02, -1.2708e-01, -3.2724e-02,\n",
      "          6.3894e-02,  1.4239e-01]], device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([ 0.0223, -0.0445, -0.0476, -0.1337,  0.1560, -0.1135, -0.0286, -0.0411,\n",
      "        -0.0052, -0.0801, -0.0104, -0.0558,  0.1265, -0.1437, -0.0892,  0.0135],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([[-0.2398, -0.0840,  0.0086, -0.2099, -0.1233, -0.0151, -0.1959,  0.1247,\n",
      "         -0.1399, -0.1643, -0.2290, -0.1551, -0.1574,  0.0790,  0.2154,  0.1678],\n",
      "        [ 0.2298, -0.0327, -0.1627, -0.2471,  0.0916, -0.0419, -0.2130, -0.2325,\n",
      "         -0.0325, -0.1180, -0.0649,  0.0709,  0.0036, -0.1256,  0.0926, -0.2038],\n",
      "        [-0.1114,  0.1413, -0.0248,  0.0397,  0.1848, -0.1302,  0.0538,  0.0570,\n",
      "          0.0194,  0.1186,  0.0694,  0.0491, -0.0972, -0.1246,  0.2099, -0.0493],\n",
      "        [-0.2076, -0.1211,  0.1516,  0.0128, -0.1250,  0.2143,  0.0461, -0.2231,\n",
      "          0.0300, -0.1413, -0.0868,  0.2130, -0.1666, -0.0444,  0.1558,  0.1953],\n",
      "        [-0.0148, -0.1253, -0.0772, -0.1010,  0.0876,  0.0245, -0.0078, -0.0521,\n",
      "         -0.0185,  0.0493,  0.2058, -0.1699, -0.0225,  0.2184,  0.0325,  0.1100],\n",
      "        [-0.1208,  0.0056,  0.2020, -0.1737,  0.2348, -0.0773, -0.2375,  0.0888,\n",
      "         -0.1040,  0.0114,  0.1391,  0.0305, -0.1742, -0.1975, -0.0941,  0.1355],\n",
      "        [-0.1357,  0.1889,  0.2011, -0.1999, -0.1661, -0.1803,  0.0463, -0.0309,\n",
      "          0.1760, -0.0764, -0.0258,  0.1747,  0.0939, -0.0076,  0.1740, -0.0642],\n",
      "        [ 0.1157, -0.1459,  0.1784,  0.0419, -0.1636, -0.1958, -0.0096, -0.0374,\n",
      "          0.1000, -0.0624, -0.1416,  0.2275, -0.0456, -0.1461,  0.2328, -0.0864]],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([-0.2127,  0.1379,  0.1302,  0.2474,  0.0080,  0.2471,  0.0082, -0.1512],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([[ 0.1708, -0.1236, -0.1303, -0.2334,  0.0324,  0.2009, -0.0171,  0.1766,\n",
      "          0.1148,  0.1173,  0.1396,  0.2376, -0.0715, -0.2281,  0.2414, -0.0929]],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([-0.0285], device='cuda:0', requires_grad=True)\n"
     ]
    }
   ],
   "source": [
    "for m in model.parameters():\n",
    "    print(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:15:18.127427Z",
     "start_time": "2020-10-21T08:15:17.121121Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.6571],\n",
      "        [0.5611],\n",
      "        [0.4242],\n",
      "        [0.3730],\n",
      "        [0.3629],\n",
      "        [0.4684],\n",
      "        [0.4846],\n",
      "        [0.5499],\n",
      "        [0.5467],\n",
      "        [0.4819],\n",
      "        [0.4654],\n",
      "        [0.3639],\n",
      "        [0.4479],\n",
      "        [0.4584],\n",
      "        [0.5055],\n",
      "        [0.4785],\n",
      "        [0.5577],\n",
      "        [0.3604],\n",
      "        [0.5382],\n",
      "        [0.4297],\n",
      "        [0.3515],\n",
      "        [0.4512],\n",
      "        [0.4706],\n",
      "        [0.4632],\n",
      "        [0.3307],\n",
      "        [0.4183],\n",
      "        [0.5258],\n",
      "        [0.5802],\n",
      "        [0.5989],\n",
      "        [0.6743],\n",
      "        [0.6863],\n",
      "        [0.4905],\n",
      "        [0.3589],\n",
      "        [0.5526],\n",
      "        [0.4963],\n",
      "        [0.5502],\n",
      "        [0.4455],\n",
      "        [0.5962],\n",
      "        [0.5092],\n",
      "        [0.5736],\n",
      "        [0.5427],\n",
      "        [0.5002],\n",
      "        [0.3664],\n",
      "        [0.5767],\n",
      "        [0.3538],\n",
      "        [0.3392],\n",
      "        [0.4803],\n",
      "        [0.3047],\n",
      "        [0.5985],\n",
      "        [0.5016],\n",
      "        [0.3084],\n",
      "        [0.6806],\n",
      "        [0.4185],\n",
      "        [0.4621],\n",
      "        [0.5474],\n",
      "        [0.5254],\n",
      "        [0.4416],\n",
      "        [0.5345],\n",
      "        [0.4581],\n",
      "        [0.3567],\n",
      "        [0.2663],\n",
      "        [0.5329],\n",
      "        [0.5550],\n",
      "        [0.6078]], device='cuda:0', grad_fn=<SigmoidBackward>)\n"
     ]
    }
   ],
   "source": [
    "# 简单测试一下模型\n",
    "for (x, y) in iter(dl_train):\n",
    "    x = x.cuda()\n",
    "    print(model(x))\n",
    "    break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型的训练与评估"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 模型评估函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:47:08.446537Z",
     "start_time": "2020-10-21T08:47:08.427624Z"
    }
   },
   "outputs": [],
   "source": [
    "# Global variables that are shared across processes\n",
    "_model = None\n",
    "_testRatings = None\n",
    "_testNegatives = None\n",
    "_K = None\n",
    "\n",
    "# HitRation\n",
    "def getHitRatio(ranklist, gtItem):\n",
    "    for item in ranklist:\n",
    "        if item == gtItem:\n",
    "            return 1\n",
    "    return 0\n",
    "\n",
    "# NDCG\n",
    "def getNDCG(ranklist, gtItem):\n",
    "    for i in range(len(ranklist)):\n",
    "        item = ranklist[i]\n",
    "        if item == gtItem:\n",
    "            return np.log(2) / np.log(i+2)\n",
    "    return 0\n",
    "\n",
    "def eval_one_rating(idx):   # 一次评分预测\n",
    "    rating = _testRatings[idx]\n",
    "    items = _testNegatives[idx]\n",
    "    u = rating[0]\n",
    "    gtItem = rating[1]\n",
    "    items.append(gtItem)\n",
    "    \n",
    "    # Get prediction scores\n",
    "    map_item_score = {}\n",
    "    users = np.full(len(items), u, dtype='int32')\n",
    "    \n",
    "    test_data = torch.tensor(np.vstack([users, np.array(items)]).T).to(device)\n",
    "    predictions = _model(test_data)\n",
    "    for i in range(len(items)):\n",
    "        item = items[i]\n",
    "        map_item_score[item] = predictions[i].data.cpu().numpy()[0]\n",
    "    items.pop()\n",
    "    \n",
    "    # Evaluate top rank list\n",
    "    ranklist = heapq.nlargest(_K, map_item_score, key=lambda k: map_item_score[k])  # heapq是堆排序算法， 取前K个\n",
    "    hr = getHitRatio(ranklist, gtItem)\n",
    "    ndcg = getNDCG(ranklist, gtItem)\n",
    "    return hr, ndcg\n",
    "\n",
    "def evaluate_model(model, testRatings, testNegatives, K):\n",
    "    \"\"\"\n",
    "    Evaluate the performance (Hit_Ratio, NDCG) of top-K recommendation\n",
    "    Return: score of each test rating.\n",
    "    \"\"\"\n",
    "    global _model\n",
    "    global _testRatings\n",
    "    global _testNegatives\n",
    "    global _K\n",
    "    \n",
    "    _model = model\n",
    "    _testNegatives = testNegatives\n",
    "    _testRatings = testRatings\n",
    "    _K = K\n",
    "    \n",
    "    hits, ndcgs = [], []\n",
    "    for idx in range(len(_testRatings)):\n",
    "        (hr, ndcg) = eval_one_rating(idx)\n",
    "        hits.append(hr)\n",
    "        ndcgs.append(ndcg)\n",
    "    return hits, ndcgs   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T00:49:17.101447Z",
     "start_time": "2020-10-21T00:49:17.083494Z"
    }
   },
   "source": [
    "### 模型的训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:47:11.041595Z",
     "start_time": "2020-10-21T08:47:11.027633Z"
    }
   },
   "outputs": [],
   "source": [
    "# 训练参数设置\n",
    "loss_func = nn.BCELoss()\n",
    "optimizer = torch.optim.Adam(params=model.parameters(), lr=lr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:48:15.746454Z",
     "start_time": "2020-10-21T08:47:13.392304Z"
    }
   },
   "outputs": [],
   "source": [
    "# 计算出初始的评估\n",
    "(hits, ndcgs) = evaluate_model(model, testRatings, testNegatives, topK)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T08:48:18.171964Z",
     "start_time": "2020-10-21T08:48:18.158002Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Init: HR=0.0983, NDCG=0.0444\n"
     ]
    }
   ],
   "source": [
    "hr, ndcg = np.array(hits).mean(), np.array(ndcgs).mean()\n",
    "print('Init: HR=%.4f, NDCG=%.4f' %(hr, ndcg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([64])\n"
     ]
    }
   ],
   "source": [
    "print(labels.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "np_label = labels.cpu().numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(64,)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_label.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [1.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [1.],\n",
       "       [0.],\n",
       "       [1.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [1.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [1.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [1.],\n",
       "       [0.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [1.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [1.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.]], dtype=float32)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_label.reshape(64, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T11:41:12.838443Z",
     "start_time": "2020-10-21T09:52:42.609480Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[step = 10000] loss: 0.418\n",
      "[step = 20000] loss: 0.391\n",
      "[step = 30000] loss: 0.382\n",
      "[step = 40000] loss: 0.377\n",
      "[step = 50000] loss: 0.373\n",
      "[step = 60000] loss: 0.371\n",
      "[step = 70000] loss: 0.369\n",
      "\n",
      "EPOCH = 0, loss = 0.368, hr = 0.451, ndcg = 0.251\n",
      "[step = 10000] loss: 0.354\n",
      "[step = 20000] loss: 0.355\n",
      "[step = 30000] loss: 0.355\n",
      "[step = 40000] loss: 0.355\n",
      "[step = 50000] loss: 0.355\n",
      "[step = 60000] loss: 0.355\n",
      "[step = 70000] loss: 0.355\n",
      "\n",
      "EPOCH = 1, loss = 0.354, hr = 0.450, ndcg = 0.249\n",
      "[step = 10000] loss: 0.350\n",
      "[step = 20000] loss: 0.351\n",
      "[step = 30000] loss: 0.351\n",
      "[step = 40000] loss: 0.351\n",
      "[step = 50000] loss: 0.351\n",
      "[step = 60000] loss: 0.351\n",
      "[step = 70000] loss: 0.351\n",
      "\n",
      "EPOCH = 2, loss = 0.351, hr = 0.465, ndcg = 0.258\n",
      "[step = 10000] loss: 0.345\n",
      "[step = 20000] loss: 0.346\n",
      "[step = 30000] loss: 0.344\n",
      "[step = 40000] loss: 0.344\n",
      "[step = 50000] loss: 0.343\n",
      "[step = 60000] loss: 0.342\n",
      "[step = 70000] loss: 0.342\n",
      "\n",
      "EPOCH = 3, loss = 0.341, hr = 0.487, ndcg = 0.272\n",
      "[step = 10000] loss: 0.332\n",
      "[step = 20000] loss: 0.332\n",
      "[step = 30000] loss: 0.332\n",
      "[step = 40000] loss: 0.331\n",
      "[step = 50000] loss: 0.331\n",
      "[step = 60000] loss: 0.331\n",
      "[step = 70000] loss: 0.331\n",
      "\n",
      "EPOCH = 4, loss = 0.330, hr = 0.493, ndcg = 0.277\n",
      "[step = 10000] loss: 0.326\n",
      "[step = 20000] loss: 0.325\n",
      "[step = 30000] loss: 0.325\n",
      "[step = 40000] loss: 0.325\n",
      "[step = 50000] loss: 0.325\n",
      "[step = 60000] loss: 0.325\n",
      "[step = 70000] loss: 0.325\n",
      "\n",
      "EPOCH = 5, loss = 0.325, hr = 0.506, ndcg = 0.286\n",
      "[step = 10000] loss: 0.322\n",
      "[step = 20000] loss: 0.321\n",
      "[step = 30000] loss: 0.322\n",
      "[step = 40000] loss: 0.321\n",
      "[step = 50000] loss: 0.321\n",
      "[step = 60000] loss: 0.321\n",
      "[step = 70000] loss: 0.321\n",
      "\n",
      "EPOCH = 6, loss = 0.321, hr = 0.521, ndcg = 0.291\n",
      "[step = 10000] loss: 0.317\n",
      "[step = 20000] loss: 0.317\n",
      "[step = 30000] loss: 0.317\n",
      "[step = 40000] loss: 0.317\n",
      "[step = 50000] loss: 0.317\n",
      "[step = 60000] loss: 0.317\n",
      "[step = 70000] loss: 0.317\n",
      "\n",
      "EPOCH = 7, loss = 0.317, hr = 0.525, ndcg = 0.297\n",
      "[step = 10000] loss: 0.313\n",
      "[step = 20000] loss: 0.313\n",
      "[step = 30000] loss: 0.313\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32mC:\\Users\\ZHONGQ~1\\AppData\\Local\\Temp/ipykernel_26548/2948591896.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     22\u001b[0m         \u001b[1;31m# 反向传播求梯度\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     23\u001b[0m         \u001b[0mloss\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 24\u001b[1;33m         \u001b[0moptimizer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     25\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     26\u001b[0m         \u001b[1;31m# 打印batch级别日志\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda3\\envs\\tfenv\\lib\\site-packages\\torch\\optim\\adam.py\u001b[0m in \u001b[0;36mstep\u001b[1;34m(self, closure)\u001b[0m\n\u001b[0;32m    105\u001b[0m                 \u001b[0mstep_size\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgroup\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'lr'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mmath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msqrt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbias_correction2\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m/\u001b[0m \u001b[0mbias_correction1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    106\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 107\u001b[1;33m                 \u001b[0mp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maddcdiv_\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mstep_size\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexp_avg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdenom\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    108\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    109\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mloss\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# 模型训练 \n",
    "best_hr, best_ndcg, best_iter = hr, ndcg, -1\n",
    "\n",
    "epochs = 10\n",
    "log_step_freq = 10000\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    \n",
    "    # 训练阶段\n",
    "    model.train()\n",
    "    loss_sum = 0.0\n",
    "    for step, (features, labels) in enumerate(dl_train, 1):\n",
    "        \n",
    "        features, labels = features.cuda(), labels.cuda()\n",
    "        # 梯度清零\n",
    "        optimizer.zero_grad()\n",
    "        \n",
    "        # 正向传播\n",
    "        predictions = model(features)\n",
    "        loss = loss_func(predictions, labels)\n",
    "        \n",
    "        # 反向传播求梯度\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        \n",
    "        # 打印batch级别日志\n",
    "        loss_sum += loss.item()\n",
    "        if step % log_step_freq == 0:\n",
    "            print((\"[step = %d] loss: %.3f\") %\n",
    "                  (step, loss_sum/step))\n",
    "    \n",
    "    # 验证阶段\n",
    "    model.eval()\n",
    "    (hits, ndcgs) = evaluate_model(model, testRatings, testNegatives, topK)\n",
    "    hr, ndcg = np.array(hits).mean(), np.array(ndcgs).mean()\n",
    "    if hr > best_hr:\n",
    "        best_hr, best_ndcg, best_iter = hr, ndcg, epoch\n",
    "        torch.save(model.state_dict(), 'Pre_train/m1-1m_NeuralMF.pkl')  \n",
    "        \n",
    "    info = (epoch, loss_sum/step, hr, ndcg)\n",
    "    print((\"\\nEPOCH = %d, loss = %.3f, hr = %.3f, ndcg = %.3f\") %info)\n",
    "print('Finished Training...') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 玩一下预训练\n",
    "预训练就是用已经训练好的参数， 直接用到NeuralMF上， 然后再进行训练， 这里考察对结构每个层参数的把握， 步骤如下：\n",
    "* 首先要建立GMF和MLP模型， 并导入保存好的参数\n",
    "* 建立NeuralMF模型， 获取到相应层， 得到相应的参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T12:20:06.981529Z",
     "start_time": "2020-10-21T12:20:06.966613Z"
    }
   },
   "outputs": [],
   "source": [
    "from GMF_MLP import GMF, MLP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:06:03.590333Z",
     "start_time": "2020-10-21T09:06:03.577368Z"
    }
   },
   "outputs": [],
   "source": [
    "gmf = GMF(num_users, num_items, num_factors)\n",
    "mlp = MLP(num_users, num_items, [num_factors*2, 64, 32, 16, 8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:06:42.364620Z",
     "start_time": "2020-10-21T09:06:42.344632Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([('MF_Embedding_User.weight',\n",
       "              tensor([[ 0.6279, -1.0892, -1.4830,  ...,  1.2806,  0.7660,  0.8838],\n",
       "                      [-1.4812,  0.2715, -0.0581,  ..., -0.3319,  1.0200,  0.2614],\n",
       "                      [-0.2170, -0.1739, -0.0279,  ..., -1.9526, -0.0041, -1.3912],\n",
       "                      ...,\n",
       "                      [-0.1122,  0.5834,  1.0698,  ..., -0.5528, -0.7011,  0.4295],\n",
       "                      [-1.5001, -0.3486, -0.7905,  ...,  1.5360,  0.3815,  1.4865],\n",
       "                      [-1.6764,  1.9348,  1.3802,  ..., -0.9602,  0.8150, -0.4911]])),\n",
       "             ('MF_Embedding_Item.weight',\n",
       "              tensor([[-0.0801, -0.3678, -0.6555,  ..., -1.2752,  1.2915,  0.4929],\n",
       "                      [ 1.1122,  0.7490,  1.8574,  ...,  0.4982, -0.2133, -0.8871],\n",
       "                      [ 1.3091,  1.2819, -0.4126,  ..., -0.7710,  0.5700,  0.7012],\n",
       "                      ...,\n",
       "                      [-0.9993, -1.4289, -0.7301,  ...,  0.2427, -0.1776,  1.5740],\n",
       "                      [ 1.1428, -0.6012,  0.0189,  ...,  0.8186, -0.2758, -1.1474],\n",
       "                      [-0.6608, -0.3778,  0.2672,  ..., -0.0941, -0.4848, -0.1870]])),\n",
       "             ('linear.weight',\n",
       "              tensor([[-0.0196, -0.2568, -0.1753,  0.0727,  0.0870,  0.2134, -0.3088,  0.2777]])),\n",
       "             ('linear.bias', tensor([0.0576]))])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gmf.state_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:07:44.261950Z",
     "start_time": "2020-10-21T09:07:44.242047Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([('MF_Embedding_User.weight',\n",
       "              tensor([[-0.5454,  1.9617,  0.0869,  ...,  0.4281,  1.8094,  0.3854],\n",
       "                      [-0.5980, -1.1430, -0.3457,  ...,  0.5913, -0.5720, -0.7922],\n",
       "                      [ 0.6597,  0.7201, -1.5586,  ..., -1.4184,  1.7707, -1.5127],\n",
       "                      ...,\n",
       "                      [ 2.0239, -0.7791,  0.4733,  ..., -1.0085, -0.7556,  2.5122],\n",
       "                      [-0.9829,  0.2878, -0.4144,  ..., -0.0494, -0.3845,  0.3127],\n",
       "                      [ 0.3841,  0.0400, -0.5352,  ...,  0.6778, -0.7824,  0.6954]])),\n",
       "             ('MF_Embedding_Item.weight',\n",
       "              tensor([[-1.5673, -0.5923,  0.3009,  ...,  0.3700,  0.2939, -1.4450],\n",
       "                      [-1.2123, -0.2765,  1.1029,  ..., -1.2877,  0.0081, -1.2377],\n",
       "                      [-1.6032, -0.3513,  0.4709,  ...,  0.4668, -1.6501,  0.3641],\n",
       "                      ...,\n",
       "                      [ 0.6977,  0.7832, -1.5199,  ...,  1.7004,  0.1634, -0.8731],\n",
       "                      [-0.6062,  0.5826, -0.2024,  ...,  0.6650, -1.0736,  0.8852],\n",
       "                      [ 0.3606,  2.5979,  2.4289,  ..., -0.0176,  0.1395,  1.0994]])),\n",
       "             ('dnn_network.0.weight',\n",
       "              tensor([[ 0.2064, -0.1505,  0.2013,  ..., -0.1020,  0.2470,  0.1839],\n",
       "                      [ 0.1216,  0.0178,  0.0545,  ..., -0.1153,  0.2134,  0.2400],\n",
       "                      [ 0.2390, -0.1458,  0.0235,  ...,  0.2448,  0.2030, -0.1952],\n",
       "                      ...,\n",
       "                      [ 0.1906,  0.0772,  0.1142,  ...,  0.1400, -0.0061, -0.1882],\n",
       "                      [ 0.1938, -0.1928, -0.1612,  ..., -0.1313, -0.1482,  0.1281],\n",
       "                      [-0.1161, -0.1733,  0.2450,  ...,  0.0285,  0.1215, -0.1659]])),\n",
       "             ('dnn_network.0.bias',\n",
       "              tensor([ 0.1612,  0.2230,  0.0767, -0.1238,  0.0717,  0.2210,  0.0731, -0.1678,\n",
       "                      -0.1885,  0.0077, -0.2147, -0.0744,  0.1256, -0.2118,  0.2446, -0.1955,\n",
       "                      -0.0964, -0.2402, -0.0986, -0.1794,  0.1998, -0.2224,  0.0655,  0.1251,\n",
       "                       0.1687,  0.2450, -0.2115,  0.1240,  0.0163,  0.0534, -0.0226, -0.2484,\n",
       "                      -0.0396,  0.0864,  0.0011, -0.0603, -0.0872, -0.0112,  0.2480, -0.0371,\n",
       "                       0.0108,  0.2473, -0.0775,  0.1106, -0.1559, -0.0295,  0.0584, -0.0813,\n",
       "                      -0.1357, -0.2488, -0.2463, -0.1633,  0.1048, -0.1471, -0.2072,  0.0901,\n",
       "                      -0.1112, -0.1435,  0.1250, -0.1129,  0.0440, -0.0573,  0.1421, -0.0704])),\n",
       "             ('dnn_network.1.weight',\n",
       "              tensor([[ 0.0799, -0.0285,  0.1216,  ..., -0.0103, -0.0355,  0.0656],\n",
       "                      [ 0.1134,  0.0966, -0.1040,  ..., -0.0081,  0.0250,  0.0213],\n",
       "                      [-0.1093, -0.1212, -0.0164,  ...,  0.0205,  0.1194,  0.0664],\n",
       "                      ...,\n",
       "                      [-0.0202, -0.0612,  0.0323,  ..., -0.0877,  0.0106,  0.0188],\n",
       "                      [-0.0318, -0.0830,  0.0241,  ..., -0.0075,  0.0981, -0.0780],\n",
       "                      [ 0.1211, -0.0512, -0.1226,  ..., -0.0642,  0.1122,  0.0279]])),\n",
       "             ('dnn_network.1.bias',\n",
       "              tensor([ 0.0928, -0.1178,  0.1023,  0.1178,  0.0974, -0.0517,  0.0185,  0.1018,\n",
       "                       0.0662,  0.0466, -0.0502, -0.0069,  0.1143,  0.0682,  0.0476,  0.0084,\n",
       "                       0.0187,  0.0195,  0.1052,  0.0795, -0.0272,  0.0928, -0.1084,  0.0999,\n",
       "                       0.0777,  0.0998,  0.0844,  0.0334,  0.0547, -0.0377,  0.0482, -0.1083])),\n",
       "             ('dnn_network.2.weight',\n",
       "              tensor([[-5.2322e-02, -6.9814e-02, -1.2841e-01,  6.3933e-02, -1.1412e-02,\n",
       "                        1.0411e-01,  1.3779e-01,  3.6906e-02, -4.4438e-02, -4.0025e-02,\n",
       "                        1.6906e-01,  1.4360e-01, -7.9913e-03, -1.4446e-01, -1.5301e-01,\n",
       "                       -1.6434e-01, -6.0576e-03, -1.2436e-01, -5.0325e-02,  1.3339e-01,\n",
       "                        6.6582e-02, -7.2795e-02, -1.0760e-01,  4.3878e-02,  4.6695e-02,\n",
       "                        5.5203e-03,  1.4541e-01,  7.2520e-02,  9.5009e-02,  1.7022e-01,\n",
       "                       -1.6615e-01, -1.6437e-01],\n",
       "                      [-4.6400e-02, -1.2309e-01, -4.0799e-02, -8.5932e-02, -8.8944e-02,\n",
       "                        6.3484e-02, -3.3145e-02, -2.7315e-02, -8.4050e-02, -1.0693e-01,\n",
       "                        1.4927e-02, -1.7485e-01,  5.6434e-02, -6.4937e-02, -6.8337e-02,\n",
       "                        1.6639e-01, -8.9857e-02,  6.0658e-03,  7.9028e-02,  1.2257e-01,\n",
       "                       -1.2364e-01,  3.5474e-02,  1.7278e-01, -9.3020e-02, -4.9105e-02,\n",
       "                        3.4858e-02, -6.5992e-02,  4.9002e-02, -4.5971e-02, -5.7668e-02,\n",
       "                        1.5148e-01, -1.1555e-01],\n",
       "                      [-1.4967e-01, -9.4938e-02,  5.9568e-02,  1.0886e-01,  1.1313e-01,\n",
       "                       -7.2622e-02, -8.1278e-03, -3.0660e-02, -5.8783e-02,  2.8276e-02,\n",
       "                       -1.3032e-01,  1.2505e-01, -5.5902e-02, -3.3694e-03,  1.4198e-01,\n",
       "                       -1.3129e-01,  1.4912e-01, -3.1605e-02, -4.7074e-02,  4.2483e-02,\n",
       "                        5.7586e-03, -1.3528e-01, -8.2305e-02, -1.3810e-01,  9.5220e-03,\n",
       "                        7.1810e-02, -1.7087e-01, -7.7120e-02, -1.7634e-01, -6.3754e-02,\n",
       "                       -7.1298e-02,  3.3823e-02],\n",
       "                      [-1.4530e-01, -2.4480e-02,  4.9033e-02,  1.4207e-01,  1.5068e-01,\n",
       "                       -8.1530e-02, -9.2241e-04, -4.1225e-02,  5.2234e-02,  1.1669e-01,\n",
       "                        3.1473e-02,  1.1899e-01, -2.8689e-03,  1.4597e-01,  8.5415e-02,\n",
       "                        1.3471e-01,  1.0956e-01,  5.0125e-02, -1.7283e-01, -1.0667e-01,\n",
       "                       -1.5433e-01, -1.4383e-01, -8.1364e-02,  1.7293e-01,  6.8227e-03,\n",
       "                        1.0128e-01, -1.6870e-01, -3.5953e-02, -1.5496e-01,  1.7562e-01,\n",
       "                       -5.5336e-02, -1.7522e-01],\n",
       "                      [-9.7087e-02,  1.0704e-01, -2.0476e-02,  1.3504e-01, -1.0633e-01,\n",
       "                        6.3711e-02,  9.0996e-02,  8.6123e-02, -1.5322e-01, -1.9781e-02,\n",
       "                        9.2763e-02,  4.8217e-02,  4.5726e-02, -7.1117e-02, -1.3567e-01,\n",
       "                        1.1419e-02, -7.3874e-02,  6.7204e-02, -4.5263e-02,  3.8700e-02,\n",
       "                       -1.0917e-01,  8.9227e-03, -7.0517e-02, -1.3697e-02,  1.4577e-01,\n",
       "                       -7.2214e-02, -5.6632e-02,  1.2229e-01, -2.2505e-02,  1.2485e-01,\n",
       "                        6.6082e-02, -1.1224e-02],\n",
       "                      [ 1.3973e-01, -1.2733e-01, -5.8917e-02, -3.4871e-02, -9.1666e-02,\n",
       "                        1.7650e-02, -1.6322e-01,  1.5503e-01,  1.1524e-01,  5.5748e-02,\n",
       "                        1.7775e-02, -5.6315e-02, -1.3618e-01,  6.7021e-03,  3.5123e-02,\n",
       "                       -1.4816e-01,  9.7243e-03,  8.7202e-02, -1.8430e-02,  6.8181e-02,\n",
       "                        5.2317e-02,  1.2564e-01, -5.7296e-02,  5.0343e-03,  1.7474e-01,\n",
       "                        1.1846e-01,  5.8915e-02, -4.8325e-02,  4.9245e-02, -3.1606e-02,\n",
       "                        1.4973e-01, -1.4255e-01],\n",
       "                      [ 3.3830e-02, -1.2569e-01,  1.0328e-01,  6.4892e-02,  3.6812e-02,\n",
       "                        2.5759e-02,  7.0212e-03, -1.1795e-01,  1.2736e-01,  4.0578e-02,\n",
       "                        3.5586e-02,  6.5492e-02, -8.9717e-02, -6.8443e-02,  1.0703e-01,\n",
       "                        1.2493e-01, -1.9966e-02, -8.4809e-02, -7.7845e-02, -2.5923e-02,\n",
       "                        1.2287e-01, -1.5475e-01,  3.1339e-02, -8.2701e-02,  9.8924e-02,\n",
       "                        9.7126e-02, -1.2585e-02,  1.5914e-01,  5.1020e-02, -1.2035e-01,\n",
       "                       -6.1290e-02, -1.2091e-01],\n",
       "                      [-4.0152e-02, -4.4253e-02, -6.5902e-02, -5.0210e-03,  6.5702e-02,\n",
       "                       -1.9130e-02, -3.6764e-02,  2.5347e-02,  1.1546e-01,  1.7472e-01,\n",
       "                        4.8248e-02, -6.5698e-02, -1.5458e-01,  3.0222e-02,  8.6310e-02,\n",
       "                        1.0974e-01,  7.2612e-02, -1.1148e-01,  1.3528e-01, -1.3589e-01,\n",
       "                       -1.0186e-01, -6.7015e-04,  1.2658e-01,  1.4063e-01,  6.4969e-02,\n",
       "                       -1.5226e-01, -1.0987e-01, -4.2416e-02,  1.4669e-01,  7.9992e-02,\n",
       "                       -3.0866e-02,  1.6303e-01],\n",
       "                      [-3.5600e-02,  1.2644e-01,  1.3674e-01, -4.8120e-02,  5.3577e-02,\n",
       "                       -1.1026e-01, -1.5118e-01,  1.0282e-06,  1.3488e-01,  6.1304e-03,\n",
       "                       -4.9645e-02,  3.5886e-02, -8.4305e-02, -1.2889e-01, -4.8845e-02,\n",
       "                        2.4622e-02, -5.5722e-02,  5.6913e-02, -2.4919e-02,  1.5977e-01,\n",
       "                        2.4504e-02, -8.0964e-04,  2.3442e-02,  3.9391e-02, -1.0812e-01,\n",
       "                        1.0291e-01,  7.0220e-02, -1.0257e-02,  1.2811e-03,  1.0077e-01,\n",
       "                        4.5473e-02, -1.7136e-01],\n",
       "                      [ 1.1851e-01,  2.4281e-02,  3.4872e-02,  1.5699e-02, -5.3931e-02,\n",
       "                        5.4610e-02,  7.2709e-02,  7.9059e-02,  1.3933e-01, -1.3974e-01,\n",
       "                       -1.5572e-01, -1.6983e-01,  1.1509e-01,  1.8947e-02, -1.7565e-01,\n",
       "                        1.2933e-01,  1.7219e-01,  6.9766e-02,  1.0837e-02, -6.8443e-02,\n",
       "                        8.7307e-03,  1.2286e-01, -6.3862e-02, -1.7754e-03, -9.2060e-02,\n",
       "                        1.3883e-01,  1.0832e-01,  1.4431e-01, -6.7966e-02, -2.8269e-02,\n",
       "                        1.4431e-01,  1.3783e-01],\n",
       "                      [-4.1573e-03, -6.6024e-02,  4.4943e-02, -1.0058e-01, -5.6653e-02,\n",
       "                       -5.3004e-02, -3.2396e-02,  1.2584e-02,  1.5092e-01, -1.7295e-01,\n",
       "                       -7.9985e-02, -1.5338e-01, -1.0049e-02,  8.8012e-02, -1.4664e-01,\n",
       "                        1.1669e-01, -4.1019e-03,  1.0272e-01, -5.8537e-02, -6.7335e-02,\n",
       "                       -1.6748e-01, -1.3439e-01, -5.8210e-02,  5.7765e-02,  3.3047e-02,\n",
       "                       -1.5627e-01, -4.0869e-02,  1.0706e-01,  1.6394e-01,  1.4093e-02,\n",
       "                        1.1301e-01,  1.5316e-01],\n",
       "                      [-1.2558e-01, -1.3281e-02, -6.5287e-03,  1.2765e-02,  4.4743e-02,\n",
       "                       -1.0992e-01, -1.2699e-01,  1.5778e-01, -1.9036e-02, -3.2785e-02,\n",
       "                        2.0349e-02, -2.6569e-02,  1.1757e-01, -8.5584e-02, -5.0476e-03,\n",
       "                       -1.6491e-01, -1.4629e-01, -1.1913e-01,  5.1954e-02, -1.5976e-01,\n",
       "                       -8.6033e-02,  6.6482e-02,  1.3230e-01,  1.5384e-02,  3.2287e-02,\n",
       "                       -1.3193e-01, -1.8833e-02,  1.8388e-02, -1.0908e-01, -1.9724e-02,\n",
       "                       -4.6287e-02,  1.7129e-01],\n",
       "                      [-1.6175e-01, -1.0585e-01,  1.6349e-01,  3.0017e-02, -4.5269e-02,\n",
       "                        1.5079e-01, -4.4556e-02,  5.0336e-02,  1.0067e-02, -1.1769e-01,\n",
       "                        1.4451e-01, -1.0100e-01, -3.0673e-02,  1.3243e-01, -4.9008e-02,\n",
       "                        7.3749e-02, -6.7820e-02, -5.9063e-02,  1.6627e-01, -1.0082e-01,\n",
       "                       -1.2228e-01, -8.3864e-02, -1.6877e-01, -1.3130e-01,  1.4530e-01,\n",
       "                       -3.6976e-02,  1.6318e-01, -8.3358e-02, -2.0380e-02,  1.6690e-01,\n",
       "                        1.4216e-01,  1.4276e-03],\n",
       "                      [-8.6754e-02,  1.1967e-01, -1.5410e-02,  1.4576e-01,  1.1545e-01,\n",
       "                        1.5420e-01,  4.3302e-02, -1.0695e-01,  3.9623e-02, -4.1046e-02,\n",
       "                        5.7656e-03,  1.3160e-01,  3.4136e-02,  1.7642e-01, -5.7764e-02,\n",
       "                       -4.6007e-02, -1.7646e-01, -1.4021e-02,  1.5470e-01, -3.6014e-02,\n",
       "                       -7.1902e-02,  1.4087e-01,  7.4806e-02,  1.3342e-01,  3.7333e-02,\n",
       "                        1.1357e-01, -5.5173e-02,  1.6728e-01, -2.4234e-02,  1.3264e-01,\n",
       "                        6.5474e-03, -1.9173e-03],\n",
       "                      [-1.2624e-01, -1.6613e-02,  1.5903e-01, -1.3285e-01,  1.1436e-01,\n",
       "                        2.2920e-03, -1.5299e-01,  1.2878e-01,  8.6937e-02, -6.8260e-02,\n",
       "                       -1.7424e-01, -1.2681e-01,  3.7237e-02,  4.0570e-02, -1.5410e-01,\n",
       "                        9.0571e-02,  4.0023e-02,  1.4942e-01, -4.9663e-02,  1.1652e-01,\n",
       "                       -1.6973e-01,  1.3216e-01,  2.7209e-03, -1.7041e-01,  8.4704e-02,\n",
       "                        1.2175e-01,  1.2311e-01,  7.2381e-03,  5.2590e-02, -7.0427e-02,\n",
       "                       -1.6173e-01,  2.0509e-02],\n",
       "                      [-7.3305e-03, -4.7573e-02, -1.1798e-01,  1.2652e-02,  5.0170e-02,\n",
       "                       -1.0989e-01, -2.6108e-02,  4.0493e-02,  1.0135e-01, -9.4626e-02,\n",
       "                       -3.2209e-02,  6.6499e-03, -1.0862e-01,  1.4882e-01,  2.2597e-02,\n",
       "                        3.0995e-02, -1.2149e-01,  8.9784e-02, -1.6671e-01, -1.4613e-01,\n",
       "                        4.8932e-02,  7.2117e-03, -5.3358e-02, -3.4927e-02,  1.4688e-01,\n",
       "                        1.4099e-01,  1.7110e-01,  2.2555e-02, -1.2266e-01, -7.9307e-03,\n",
       "                        1.1759e-01, -2.9584e-02]])),\n",
       "             ('dnn_network.2.bias',\n",
       "              tensor([ 0.0807, -0.0385,  0.0504, -0.1046,  0.1363, -0.0359, -0.1379,  0.1679,\n",
       "                       0.0856,  0.1494,  0.1112, -0.1398,  0.1494, -0.0943, -0.1595, -0.0177])),\n",
       "             ('dnn_network.3.weight',\n",
       "              tensor([[ 0.1040,  0.1257,  0.2031,  0.0264, -0.1223, -0.2183, -0.0419,  0.2260,\n",
       "                       -0.1036,  0.2492,  0.1181, -0.1558, -0.0736,  0.2194, -0.0604, -0.1279],\n",
       "                      [ 0.0362, -0.0493, -0.0577,  0.1441,  0.0416, -0.0932,  0.1489,  0.0921,\n",
       "                       -0.2261, -0.0004,  0.1687, -0.1185, -0.0194, -0.1242,  0.1587, -0.0725],\n",
       "                      [-0.1299,  0.0789, -0.1348,  0.0309, -0.1821,  0.2119, -0.0281, -0.0920,\n",
       "                        0.0475, -0.1773, -0.2285, -0.0806,  0.1966, -0.1944,  0.2168,  0.0507],\n",
       "                      [-0.2052,  0.0055, -0.1388, -0.0688, -0.0643, -0.0161, -0.1250, -0.2405,\n",
       "                       -0.0582,  0.0277,  0.1914, -0.1018, -0.0403, -0.1809, -0.1745, -0.1565],\n",
       "                      [ 0.1259,  0.0434, -0.0936, -0.2103, -0.2456,  0.2254,  0.0591, -0.1964,\n",
       "                        0.1535,  0.1521, -0.0193, -0.0041, -0.2395,  0.1167,  0.1761,  0.2181],\n",
       "                      [ 0.0165,  0.0392,  0.1975,  0.1990, -0.2003,  0.1869, -0.1370, -0.1654,\n",
       "                       -0.2113, -0.0498,  0.1527, -0.1895, -0.0401,  0.2301, -0.1661,  0.1382],\n",
       "                      [ 0.1039, -0.1502, -0.1288, -0.1297, -0.0392, -0.2362,  0.0299,  0.0119,\n",
       "                        0.0700,  0.0699, -0.1072, -0.0127,  0.0203, -0.1106,  0.0721,  0.1506],\n",
       "                      [ 0.1748,  0.0997,  0.2467, -0.1412, -0.0218, -0.1031,  0.0510,  0.0206,\n",
       "                        0.1945,  0.1845,  0.0245, -0.1841,  0.1563,  0.1407,  0.0800, -0.0410]])),\n",
       "             ('dnn_network.3.bias',\n",
       "              tensor([ 0.1655,  0.0518, -0.0567, -0.1427,  0.0324, -0.2464, -0.0770, -0.1284])),\n",
       "             ('linear.weight',\n",
       "              tensor([[ 0.2064,  0.2969,  0.2968, -0.2318, -0.0633, -0.0721,  0.2133, -0.1551]])),\n",
       "             ('linear.bias', tensor([0.3162]))])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mlp.state_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:09:27.212472Z",
     "start_time": "2020-10-21T09:09:27.185545Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<All keys matched successfully>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下面导入保存好的参数\n",
    "gmf.load_state_dict(torch.load('Pre_train/m1-1m_GMF.pkl'))\n",
    "mlp.load_state_dict(torch.load('Pre_train/m1-1m_MLP.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:12:01.127622Z",
     "start_time": "2020-10-21T09:12:01.111665Z"
    }
   },
   "outputs": [],
   "source": [
    "# 下面建立NeuralMF 模型\n",
    "neural_mf = NeuralMF(num_users, num_items, num_factors, layers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:12:57.646423Z",
     "start_time": "2020-10-21T09:12:57.627438Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "odict_keys(['MF_Embedding_User.weight', 'MF_Embedding_Item.weight', 'MLP_Embedding_User.weight', 'MLP_Embedding_Item.weight', 'dnn_network.0.weight', 'dnn_network.0.bias', 'dnn_network.1.weight', 'dnn_network.1.bias', 'dnn_network.2.weight', 'dnn_network.2.bias', 'linear.weight', 'linear.bias', 'linear2.weight', 'linear2.bias'])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neural_mf.state_dict().keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:32:12.999140Z",
     "start_time": "2020-10-21T09:32:12.985179Z"
    }
   },
   "outputs": [],
   "source": [
    "old_param = neural_mf.state_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:44:51.010701Z",
     "start_time": "2020-10-21T09:44:51.005670Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "odict_keys(['MF_Embedding_User.weight', 'MF_Embedding_Item.weight', 'linear.weight', 'linear.bias'])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gmf.state_dict().keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:33:58.109071Z",
     "start_time": "2020-10-21T09:33:58.102047Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "odict_keys(['MF_Embedding_User.weight', 'MF_Embedding_Item.weight', 'MLP_Embedding_User.weight', 'MLP_Embedding_Item.weight', 'dnn_network.0.weight', 'dnn_network.0.bias', 'dnn_network.1.weight', 'dnn_network.1.bias', 'dnn_network.2.weight', 'dnn_network.2.bias', 'linear.weight', 'linear.bias', 'linear2.weight', 'linear2.bias'])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "old_param.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:35:14.285831Z",
     "start_time": "2020-10-21T09:35:14.273819Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "odict_keys(['MF_Embedding_User.weight', 'MF_Embedding_Item.weight', 'dnn_network.0.weight', 'dnn_network.0.bias', 'dnn_network.1.weight', 'dnn_network.1.bias', 'dnn_network.2.weight', 'dnn_network.2.bias', 'dnn_network.3.weight', 'dnn_network.3.bias', 'linear.weight', 'linear.bias'])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mlp.state_dict().keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:50:28.589241Z",
     "start_time": "2020-10-21T09:50:28.580265Z"
    }
   },
   "outputs": [],
   "source": [
    "old_param['MF_Embedding_User.weight'] = gmf.state_dict().get('MF_Embedding_User.weight')\n",
    "old_param['MF_Embedding_Item.weight'] = gmf.state_dict().get('MF_Embedding_Item.weight')\n",
    "old_param['MLP_Embedding_User.weight'] = mlp.state_dict().get('MF_Embedding_User.weight')\n",
    "old_param['MLP_Embedding_Item.weight'] = mlp.state_dict().get('MF_Embedding_Item.weight')\n",
    "\n",
    "for i in range(3):\n",
    "    old_param['dnn_network.' + str(i) + '.weight'] = mlp.state_dict().get('dnn_network.' + str(i) + '.weight')\n",
    "    old_param['dnn_network.' + str(i) + '.bias'] = mlp.state_dict().get('dnn_network.' + str(i) + '.bias')\n",
    "\n",
    "# 最后一层的参数， 两个模型需要一个alpha来划分比例 但是我这里最后一层两个模型的方式不一致了， 所以这里维度对不上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:42:38.948343Z",
     "start_time": "2020-10-21T09:42:38.917424Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([('MF_Embedding_User.weight',\n",
       "              tensor([[-10.9049,   2.0719,   4.0300,  ...,  -3.8390, -14.3656,   2.9681],\n",
       "                      [ -0.7090,  -1.1438,   3.7491,  ...,  -6.2249,  -8.4540,   4.3511],\n",
       "                      [ -8.5488,  -4.4219,   4.3419,  ...,  -9.4483,  -4.8121,   2.1526],\n",
       "                      ...,\n",
       "                      [ -8.2142,  -2.5525,   3.6702,  ...,  -4.8879, -10.4885,   0.7474],\n",
       "                      [ -5.4971,   1.8283,   3.5808,  ...,  -8.7869, -18.5828,   3.7338],\n",
       "                      [ -2.2635,  -7.6829,   2.3591,  ...,  -1.6704, -12.4867,   1.1410]])),\n",
       "             ('MF_Embedding_Item.weight',\n",
       "              tensor([[  1.5389,  -3.9638,   1.7898,  ...,   1.7804,   6.2358,  -0.5696],\n",
       "                      [ -4.0041,   3.1085,  -0.4557,  ...,  10.5345,  -0.4570,   4.2654],\n",
       "                      [  4.9035,   5.8368,   1.4269,  ...,   4.1320,   5.8839,  -1.8388],\n",
       "                      ...,\n",
       "                      [ 24.8963,  21.7012, -18.6698,  ...,  37.9214, -30.0673,  17.4988],\n",
       "                      [ 24.3937,  22.5349, -17.3940,  ...,  40.3869, -28.5746,  16.3597],\n",
       "                      [ 25.3557,  21.3686, -17.5266,  ...,  37.7154, -28.8656,  17.3751]])),\n",
       "             ('MLP_Embedding_User.weight',\n",
       "              tensor([[ 4.9950, -6.9767,  5.3551,  ...,  5.7589, -7.3663, -5.6092],\n",
       "                      [ 4.8528, -4.5118,  5.2388,  ...,  3.0570, -8.8941, -4.2340],\n",
       "                      [ 7.3539, -8.0234,  7.4366,  ...,  8.1099, -6.6147, -1.7349],\n",
       "                      ...,\n",
       "                      [ 7.1080, -6.4246,  5.2171,  ...,  4.6690, -8.0232, -4.8722],\n",
       "                      [ 6.2267, -4.4847,  3.1904,  ...,  5.8430, -7.1987, -8.3548],\n",
       "                      [ 6.2948, -5.2223,  2.8955,  ...,  2.7126, -7.0047, -4.7766]])),\n",
       "             ('MLP_Embedding_Item.weight',\n",
       "              tensor([[ -7.6054,  -1.8106,   3.5323,  ...,   1.4687,  -0.0955,   0.1854],\n",
       "                      [  1.0725,  -0.7402,   3.8252,  ...,   2.2107,  -3.3219,   0.5598],\n",
       "                      [  1.3406,   0.8860,   1.6785,  ...,  -2.9676,  -0.1690,  -1.2765],\n",
       "                      ...,\n",
       "                      [ 34.6034,  37.2670,  72.4326,  ...,  76.0341,  21.0036,  15.3670],\n",
       "                      [ 66.4463,  23.0013, -20.5897,  ...,   4.5070, -62.4054, -13.1060],\n",
       "                      [ 22.3832,  29.0571, -39.4070,  ...,  76.2729, -40.6507,  33.2883]])),\n",
       "             ('dnn_network.0.weight',\n",
       "              tensor([[ 0.4368,  1.7973, -0.5419,  ...,  1.7270,  0.0888, -1.6420],\n",
       "                      [ 0.5062,  0.2336, -1.9969,  ..., -0.3831, -0.6298,  0.2523],\n",
       "                      [ 0.2225, -1.3596,  0.3727,  ..., -1.4107,  0.2095,  1.0465],\n",
       "                      ...,\n",
       "                      [ 0.3650, -0.6041,  0.5366,  ...,  0.4176, -0.0066, -0.2458],\n",
       "                      [ 0.0717, -0.9904, -0.9272,  ..., -1.2634,  1.7401, -1.4285],\n",
       "                      [ 0.7536, -1.2126, -0.3548,  ...,  0.3725, -0.0333, -0.5585]])),\n",
       "             ('dnn_network.0.bias',\n",
       "              tensor([-3.5007,  6.3196, -0.1677, -6.3300, -6.3476, -9.1748, -3.4646,  1.7153,\n",
       "                      -3.5787, -2.2585, -1.3778,  3.1100, -0.1065, -5.7601, -3.9592, -0.1380,\n",
       "                       0.7818,  1.7002,  0.3720,  4.9113,  1.5970, -4.0904,  0.8415, -2.9764,\n",
       "                      -1.4151, -3.6436, -7.1392, -2.2284, -0.9216, -3.3673, -1.6228, -4.1291,\n",
       "                      -6.1717, -1.1169, -3.1710, -0.5146, -6.4493, -0.0430, -0.4656, -3.5691,\n",
       "                      -5.5052, -0.6032, -2.1099, -0.9976, -1.7648,  0.4023, -5.9464, -7.1766,\n",
       "                      -0.4538, -4.6623,  1.5147,  1.1372, -2.6150,  0.3051, -1.7698, -4.1466,\n",
       "                      -2.3126, -6.5316, -1.1637, -0.3540, -3.3683, -8.9138,  2.2559, -2.9516])),\n",
       "             ('dnn_network.1.weight',\n",
       "              tensor([[-1.7730,  0.2150, -0.4904,  ..., -0.5599,  0.2608,  0.1584],\n",
       "                      [-0.3964,  0.8510, -0.1182,  ...,  0.8892,  0.6909,  0.2816],\n",
       "                      [-0.1679, -0.1734, -0.1613,  ..., -0.0971, -0.1580, -0.0425],\n",
       "                      ...,\n",
       "                      [-2.4966, -0.9750, -1.3330,  ...,  2.0053, -0.8791,  1.2811],\n",
       "                      [-0.0567, -0.3060, -0.2323,  ..., -0.0489, -0.0565, -0.1036],\n",
       "                      [ 0.0722,  0.3841, -0.0519,  ...,  0.6684,  0.6451, -0.9465]])),\n",
       "             ('dnn_network.1.bias',\n",
       "              tensor([-5.1443, -2.2667, -0.2815, -6.6957, -5.1257, -6.8520, -1.0225, -5.4206,\n",
       "                      -0.1870, -3.7691, -1.9530,  1.7917, -3.2754, -0.8683, -9.9854,  4.9422,\n",
       "                       1.2290,  6.5169,  1.1433, -2.1813,  1.7972, -2.7156, -3.9621, -3.4875,\n",
       "                      -3.9099,  0.3950, -0.6878, -4.0175,  4.7664,  0.5753, -0.2987, -4.2293])),\n",
       "             ('dnn_network.2.weight',\n",
       "              tensor([[-4.4051e-02, -8.1934e-01,  4.5098e-03, -7.2131e-02, -6.4582e-01,\n",
       "                        4.3078e-01,  4.3461e-01,  5.9002e-01, -3.1858e-03, -2.8823e-01,\n",
       "                       -3.8492e-02, -2.7279e-01, -5.8295e-01,  9.0764e-01, -2.1838e-01,\n",
       "                        2.4018e-01,  7.6393e-01,  8.8929e-01, -2.5254e-01, -2.8079e-01,\n",
       "                       -2.7408e-01, -9.3145e-01, -5.3054e-01, -6.0861e-01, -1.1109e-02,\n",
       "                       -4.1128e-01, -1.8452e-02, -9.3227e-02,  1.5636e-01,  7.8228e-01,\n",
       "                        8.2843e-02, -9.5167e-01],\n",
       "                      [-3.9500e-01, -7.9967e-01,  2.1564e-02, -6.1640e-01, -6.2433e-01,\n",
       "                        1.5546e+00,  1.1757e+00,  7.1575e-01, -2.3042e-02,  1.3887e+00,\n",
       "                       -5.7029e-01, -1.9144e-01, -5.1052e-01, -8.4609e-01, -7.6374e-01,\n",
       "                        1.1316e+00,  9.4472e-01, -3.4024e-03, -4.1610e-01, -2.5122e-01,\n",
       "                        6.8861e-01, -1.3120e-01, -1.5077e+00, -9.2809e-01, -5.0214e-01,\n",
       "                       -8.9124e-01, -2.3927e-01, -2.6621e-01,  1.0912e+00,  9.7414e-01,\n",
       "                       -8.5836e-02, -1.1813e+00],\n",
       "                      [ 9.1263e-01,  4.2344e-01, -4.6091e-02,  6.0063e-01,  6.1244e-01,\n",
       "                       -6.7338e-01, -1.3431e+00, -2.0414e-01, -4.9257e-02, -1.2823e+00,\n",
       "                        7.6319e-01,  4.4577e-02,  1.5136e-01,  1.1289e+00,  9.7425e-01,\n",
       "                       -1.0469e+00, -5.5425e-01,  4.4282e-01,  1.3582e-01,  8.3506e-01,\n",
       "                       -1.0269e+00,  5.6188e-01,  1.1865e+00,  1.0896e+00,  9.4982e-01,\n",
       "                        6.0999e-01, -7.5473e-02,  7.3114e-01, -1.1561e+00, -7.0813e-01,\n",
       "                       -6.0458e-02,  4.0971e-01],\n",
       "                      [-1.5414e-01, -2.9913e-01,  1.0456e-01, -6.1169e-01,  4.8296e-01,\n",
       "                        2.0147e+00, -1.8886e-01,  8.1915e-02,  1.3655e-01, -1.6800e-01,\n",
       "                       -6.6916e-01, -1.3672e+00, -1.0149e+00,  1.9609e+00, -2.0252e-01,\n",
       "                       -2.8055e-01,  9.1824e-01, -1.3270e+00, -4.5422e-01, -1.4322e+00,\n",
       "                        4.3314e-01, -6.6243e-01, -2.0089e-01,  2.8031e-01, -1.1099e-01,\n",
       "                       -2.0442e-02,  7.1619e-01,  7.5591e-01, -9.4984e-01,  1.5441e+00,\n",
       "                        1.3560e-01,  4.9008e-01],\n",
       "                      [-3.2995e-01,  3.7215e-01, -2.8340e-03,  6.8691e-02, -2.1678e-01,\n",
       "                       -1.8338e-01,  4.8511e-01,  1.0694e+00, -1.0057e-02,  1.1135e+00,\n",
       "                        1.7062e-01, -6.1651e-02, -4.5095e-01,  1.1148e+00, -5.9234e-01,\n",
       "                       -5.2451e-01, -2.9269e-01, -1.5681e+00, -3.0650e-01, -5.1542e-01,\n",
       "                       -3.5460e-01, -5.2901e-02,  5.7919e-02, -1.0059e-02, -6.5175e-02,\n",
       "                       -2.1371e-01, -2.5414e-01, -2.8552e-01, -3.4066e-01,  1.6789e+00,\n",
       "                       -2.9424e-02,  3.3820e-01],\n",
       "                      [ 1.3778e+00, -1.6648e+00, -3.3236e-02, -4.6325e-01, -4.1322e-01,\n",
       "                        5.9705e-02,  2.4918e-01, -8.3488e-01,  1.8945e-03, -3.6855e-01,\n",
       "                        9.3135e-01, -9.1142e-01,  8.5903e-01,  3.3044e+00, -1.4469e+00,\n",
       "                       -1.1948e+00, -3.2011e-02, -2.9612e-01, -3.4531e-01,  6.5727e-01,\n",
       "                       -6.9806e-01,  1.4933e+00,  1.9093e+00,  6.3497e-01,  4.3956e-01,\n",
       "                        5.8230e-01, -2.1951e-04,  4.9304e-01, -4.9308e-01, -5.4735e-01,\n",
       "                       -2.1270e-01,  1.3244e+00],\n",
       "                      [-2.6208e-01, -1.2058e+00,  8.1391e-02, -2.0514e+00,  6.4715e-02,\n",
       "                        1.0197e+00, -1.9273e-01,  6.3296e-01, -8.9678e-02,  9.1543e-01,\n",
       "                       -1.2285e-01, -1.0416e+00, -1.2093e+00,  1.8762e+00,  5.0103e-01,\n",
       "                        2.0818e-01, -8.0986e-01, -4.3927e-01, -1.8285e+00, -6.5149e-01,\n",
       "                       -1.2800e-01,  9.6707e-01, -6.9060e-01,  6.0702e-03,  1.5932e+00,\n",
       "                       -9.5766e-01,  4.3451e-01,  1.5812e+00,  5.8654e-01,  4.0028e-01,\n",
       "                       -1.7357e-01,  2.1995e+00],\n",
       "                      [ 3.3405e-01, -1.1557e+00, -9.7402e-02, -5.0923e-02, -1.4550e-01,\n",
       "                        7.7199e-01,  6.5861e-01,  7.2618e-01, -9.5417e-02, -7.7634e-01,\n",
       "                       -5.8917e-02, -9.5223e-01, -7.3283e-01,  6.2620e-01, -1.0915e+00,\n",
       "                        9.3632e-01,  5.7244e-01, -5.2406e-01, -7.7885e-01,  9.2394e-02,\n",
       "                       -1.1682e+00,  3.2472e-01,  4.4773e-01,  1.5735e-01,  6.7605e-01,\n",
       "                       -7.1062e-02, -2.2116e-02,  8.8030e-01, -1.3553e-01,  1.2833e+00,\n",
       "                       -2.4388e-01, -4.4657e-01],\n",
       "                      [-1.6452e-01,  1.0688e+00,  9.4670e-02,  2.5441e-01,  9.1669e-02,\n",
       "                        3.8534e-01, -2.4009e+00,  6.3380e-01,  1.6183e-02, -4.8297e-01,\n",
       "                       -1.1561e+00, -1.4645e+00,  1.2467e+00,  1.0534e+00,  5.4793e-01,\n",
       "                       -4.0388e-01,  3.4412e-01, -1.6244e+00, -1.0683e+00, -2.1446e-01,\n",
       "                       -9.0056e-01,  8.3586e-01,  7.5266e-01, -8.8870e-01,  1.2376e+00,\n",
       "                        5.4283e-01,  4.2756e-01,  1.7335e+00, -3.8808e-01, -9.2259e-03,\n",
       "                        1.1269e-01,  1.5592e+00],\n",
       "                      [-4.4713e-02, -6.4907e-02,  2.4254e-02, -1.2601e-01, -4.4313e-01,\n",
       "                       -6.4218e-02, -2.6300e-01, -8.1047e-02, -1.5160e-01, -6.9007e-02,\n",
       "                       -3.2775e-02, -2.8450e-01, -5.7191e-01,  9.8572e-02,  9.3093e-02,\n",
       "                       -2.5621e-01, -5.1281e-02, -3.9777e-01, -2.0278e-01, -6.2740e-01,\n",
       "                       -2.1534e-01, -2.2273e-01, -7.7451e-03, -6.5237e-01, -5.0229e-02,\n",
       "                       -3.0370e-01, -1.1697e-01, -2.2330e-01, -4.8089e-01, -1.0627e-01,\n",
       "                       -9.4544e-02,  2.9706e-01],\n",
       "                      [ 1.3601e+00,  1.3384e+00,  9.4160e-02, -1.7131e-01,  5.5850e-01,\n",
       "                       -1.8411e-02,  3.8783e-01,  1.2751e+00, -4.6852e-02,  2.9104e-01,\n",
       "                        2.3390e-01, -2.7177e-01,  2.2739e-01,  6.6171e-01,  2.0763e-02,\n",
       "                       -1.7252e-01, -4.1478e-01, -3.2555e-01, -6.2022e-01, -7.8405e-01,\n",
       "                       -1.7572e+00, -7.6649e-01, -1.1945e+00, -3.1329e-01, -2.7036e-01,\n",
       "                        2.3988e-01,  2.8656e-01,  4.3302e-01, -6.6478e-01, -3.2320e-01,\n",
       "                       -6.6820e-02,  1.8977e+00],\n",
       "                      [ 4.5706e-02,  1.9536e-01, -1.7208e-01,  8.0733e-01, -5.3704e-02,\n",
       "                        6.0554e-01, -3.0538e-01,  1.9226e-02,  7.2696e-02, -6.4441e-01,\n",
       "                        8.3836e-01, -6.0982e-01, -2.6637e-01,  1.7378e+00, -3.2646e-01,\n",
       "                       -1.0202e+00, -1.0150e+00, -1.1867e-01, -8.1304e-01,  1.2470e+00,\n",
       "                       -4.4726e-01, -1.7653e-01, -3.7283e-01, -5.6710e-01,  1.0258e+00,\n",
       "                       -3.9244e-01,  1.0244e-01,  8.6848e-01, -8.0510e-01, -6.4596e-01,\n",
       "                       -2.3615e-01,  6.3139e-02],\n",
       "                      [ 2.2190e-01,  1.9679e+00, -1.3971e-01, -8.3897e-01,  4.1391e-02,\n",
       "                       -1.5867e-01, -2.4236e+00,  9.6721e-02, -1.4513e-01, -1.1553e+00,\n",
       "                       -1.3676e+00, -8.5156e-01, -2.6498e-02,  2.9308e+00,  1.8696e+00,\n",
       "                       -5.5079e-01, -2.8262e+00, -2.8420e+00, -9.4313e-01,  4.2039e-01,\n",
       "                        2.7546e-01,  9.5783e-01,  1.2222e-01, -2.2993e-01,  1.4012e+00,\n",
       "                       -1.7311e+00, -2.1596e-01,  4.7301e+00, -9.6120e-01,  1.0940e+00,\n",
       "                        1.1745e-01,  3.8736e+00],\n",
       "                      [-2.4444e-01, -7.1277e+00, -8.1993e-02, -1.1204e+00,  1.4396e+00,\n",
       "                       -2.3764e-01,  2.1808e+00, -3.4685e+00, -1.0268e-01,  6.4083e-01,\n",
       "                       -7.6345e-01, -6.0523e-01,  3.0098e-01,  8.5429e-01,  4.9566e-01,\n",
       "                       -8.9938e-01,  5.0635e-01, -1.6473e+00, -7.4683e-01, -2.1837e-01,\n",
       "                        1.5698e-01,  9.9786e-01, -2.1973e+00,  3.1748e-01, -3.3872e+00,\n",
       "                       -2.5172e-02,  3.0819e-01, -2.1796e+00, -1.2126e+00, -4.1022e-01,\n",
       "                       -1.5058e-01,  6.7523e-01],\n",
       "                      [ 3.3748e-01,  4.0115e-01, -1.5425e-01,  5.1557e-02,  7.1352e-02,\n",
       "                        3.2070e-01, -1.9699e-01, -8.4321e-03, -1.0195e-01, -5.1266e-01,\n",
       "                       -1.7445e-01, -6.6549e-01, -2.8901e-01,  1.4721e+00,  4.2606e-01,\n",
       "                       -8.4854e-01, -2.1581e-01, -1.3488e+00, -1.0471e+00, -1.9348e-01,\n",
       "                       -4.9957e-01,  8.8806e-01,  1.1979e+00, -3.2216e-02,  1.7165e+00,\n",
       "                       -1.0433e+00,  2.3239e-01,  1.3549e+00, -1.1514e+00,  3.6782e-01,\n",
       "                        5.3545e-02,  2.1251e+00],\n",
       "                      [ 2.7569e-01, -1.7855e+00,  2.5942e-02, -5.4490e-01, -1.4425e+00,\n",
       "                        6.1914e-01,  4.0906e-01,  1.1861e+00, -9.2218e-02, -5.4406e-02,\n",
       "                       -1.0434e+00, -5.2320e-01, -1.7078e+00, -3.1370e-01, -2.9468e-01,\n",
       "                       -4.9601e-01,  3.9774e-01,  1.0367e+00, -1.2951e+00, -5.6330e-01,\n",
       "                       -4.8770e-01, -7.0061e-01, -4.7665e-01, -3.5500e-02, -1.3213e+00,\n",
       "                       -6.0825e-01, -3.6014e-01,  1.1299e+00,  7.2811e-02, -9.8238e-02,\n",
       "                        1.8612e-02,  3.0124e-01]])),\n",
       "             ('dnn_network.2.bias',\n",
       "              tensor([11.5392,  3.1231, -1.4879, -6.0367, -4.2835, -0.6179,  0.5533, -1.4626,\n",
       "                      -5.2971, -0.9771, -6.9666, -5.0035, -5.3833, -4.3558, -2.7910,  0.9846])),\n",
       "             ('linear.weight',\n",
       "              tensor([[-0.1959,  0.1101, -0.0505,  0.1351,  0.0071, -0.2161,  0.0428, -0.0438,\n",
       "                       -0.1461, -0.0510,  0.0076,  0.0810,  0.0691,  0.0465, -0.0924, -0.2010],\n",
       "                      [ 0.0031, -0.0235, -0.2369, -0.1398, -0.0676,  0.0101,  0.0867,  0.2227,\n",
       "                       -0.0370,  0.0638,  0.2261, -0.1193,  0.1833, -0.1832, -0.1811, -0.0157],\n",
       "                      [ 0.0620, -0.1796,  0.2485,  0.0471, -0.1951,  0.0709,  0.2297, -0.1861,\n",
       "                       -0.0549, -0.0087,  0.1507,  0.1785, -0.0207, -0.1235,  0.2035,  0.0715],\n",
       "                      [ 0.1917,  0.1836,  0.0753,  0.2479,  0.1660,  0.0745, -0.0601, -0.2500,\n",
       "                        0.1280, -0.0937, -0.1300,  0.1647, -0.0758,  0.1165, -0.0901,  0.2497],\n",
       "                      [-0.0525,  0.1481, -0.0251,  0.0788,  0.1222,  0.0094, -0.0815, -0.0930,\n",
       "                        0.2086, -0.0351, -0.0703, -0.0480, -0.1166,  0.2079,  0.2472,  0.0595],\n",
       "                      [ 0.1493,  0.2017,  0.2043,  0.1859, -0.1139,  0.2451,  0.0447, -0.1753,\n",
       "                        0.0480,  0.0052,  0.1732, -0.2001,  0.2412,  0.0939,  0.1235,  0.2449],\n",
       "                      [-0.1563,  0.2000, -0.0650, -0.0652, -0.0915,  0.1123,  0.0482, -0.2144,\n",
       "                        0.1981,  0.1544,  0.1573,  0.1674,  0.1677,  0.2026, -0.2411, -0.2416],\n",
       "                      [-0.1422,  0.0910, -0.0963,  0.1854,  0.1049,  0.1089, -0.2278,  0.0683,\n",
       "                        0.1335, -0.1057, -0.1886, -0.1814,  0.0575, -0.1289,  0.1955, -0.0016]])),\n",
       "             ('linear.bias',\n",
       "              tensor([ 0.0601, -0.1838,  0.1706, -0.1394, -0.0611, -0.0288,  0.1872, -0.1807])),\n",
       "             ('linear2.weight',\n",
       "              tensor([[-0.2243, -0.1967, -0.1198,  0.1907, -0.1828, -0.0936, -0.2042,  0.0292,\n",
       "                       -0.0934, -0.0588, -0.1052,  0.1138, -0.2149,  0.0086,  0.1363, -0.1850]])),\n",
       "             ('linear2.bias', tensor([-0.0011]))])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "old_param"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:40:04.935662Z",
     "start_time": "2020-10-21T09:40:04.903746Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([('MF_Embedding_User.weight',\n",
       "              tensor([[ 0.8996, -1.0869, -0.5185,  ..., -2.1004,  0.2913,  0.9657],\n",
       "                      [-0.3456, -0.6880, -0.7943,  ..., -0.1422, -0.3804,  0.5994],\n",
       "                      [-1.1899, -0.9722,  0.7825,  ..., -1.6434,  1.4448, -0.8690],\n",
       "                      ...,\n",
       "                      [-0.6792, -0.7107, -1.0414,  ..., -0.0736,  1.9246,  0.8537],\n",
       "                      [ 0.4429,  0.8433,  0.4007,  ...,  0.4654, -0.1879,  0.5628],\n",
       "                      [-0.1354, -1.3665,  1.0789,  ..., -0.7139, -0.3299, -0.7006]])),\n",
       "             ('MF_Embedding_Item.weight',\n",
       "              tensor([[-0.2368, -0.8478,  0.8295,  ...,  1.6162,  1.3757, -0.1032],\n",
       "                      [ 0.8535,  0.3204, -0.4572,  ...,  1.1362, -0.6789, -0.2666],\n",
       "                      [-1.3712,  0.3528,  0.1000,  ..., -0.6342,  0.1856,  1.0360],\n",
       "                      ...,\n",
       "                      [-1.0873,  0.7209,  0.2486,  ...,  0.7014,  1.2417,  1.3204],\n",
       "                      [-0.9949, -0.6887, -0.3439,  ...,  0.7898, -0.6685,  0.0740],\n",
       "                      [ 0.9691, -0.6192,  1.0815,  ..., -0.2122, -0.8006,  0.4417]])),\n",
       "             ('MLP_Embedding_User.weight',\n",
       "              tensor([[-1.3640,  1.5219,  0.3045,  ...,  0.0839,  0.3958,  0.3432],\n",
       "                      [-0.8664,  0.8000,  1.0148,  ...,  1.2506,  0.2915,  0.9383],\n",
       "                      [ 0.3431,  0.0980, -0.3403,  ..., -0.3428, -1.5074, -0.1978],\n",
       "                      ...,\n",
       "                      [-1.0901,  0.1561, -0.1957,  ..., -0.7934, -1.1449,  0.4708],\n",
       "                      [-1.6454, -1.7207, -2.1838,  ..., -0.4931, -0.7224, -0.3257],\n",
       "                      [ 1.3620,  1.6567, -0.7920,  ..., -0.4809,  0.1601, -0.1294]])),\n",
       "             ('MLP_Embedding_Item.weight',\n",
       "              tensor([[-0.6228, -0.4913,  0.7244,  ..., -0.2695,  0.8942, -0.0936],\n",
       "                      [ 1.4111, -0.2388, -0.8960,  ...,  0.2344, -0.4845,  0.2421],\n",
       "                      [ 1.4466, -0.7793,  1.8700,  ...,  0.4532,  1.3163, -1.7472],\n",
       "                      ...,\n",
       "                      [-0.1470, -0.2354,  2.4731,  ..., -0.8223,  0.0189, -0.9254],\n",
       "                      [-0.6713,  0.6345, -0.8244,  ...,  0.2201,  1.4866, -0.3896],\n",
       "                      [-0.0271, -0.3175,  1.1608,  ..., -0.8481,  0.5346, -0.6373]])),\n",
       "             ('dnn_network.0.weight',\n",
       "              tensor([[-0.1847, -0.1223,  0.2158,  ...,  0.2085, -0.1984, -0.0366],\n",
       "                      [ 0.0112, -0.1134,  0.1970,  ..., -0.1144, -0.1075, -0.0101],\n",
       "                      [ 0.0586, -0.1719, -0.2331,  ...,  0.0567, -0.0939, -0.2417],\n",
       "                      ...,\n",
       "                      [ 0.1110,  0.0471, -0.0245,  ..., -0.0166,  0.2364, -0.0074],\n",
       "                      [-0.1501, -0.1758, -0.0186,  ..., -0.2202, -0.1535, -0.1479],\n",
       "                      [-0.0749, -0.2472,  0.0825,  ..., -0.2378,  0.2248, -0.0930]])),\n",
       "             ('dnn_network.0.bias',\n",
       "              tensor([ 0.2220,  0.0994, -0.0012, -0.1588, -0.1011,  0.0596, -0.1051, -0.1869,\n",
       "                      -0.1142, -0.0407,  0.0059,  0.0173, -0.1412,  0.1962, -0.1615,  0.1505,\n",
       "                      -0.2355, -0.0310, -0.2233, -0.1040, -0.2458, -0.2437,  0.1844, -0.0150,\n",
       "                       0.2025,  0.0522,  0.2065,  0.0658, -0.2248,  0.2153,  0.2046, -0.1897,\n",
       "                       0.1225, -0.0115,  0.2273, -0.0741,  0.0916,  0.0538, -0.1987, -0.0353,\n",
       "                       0.2374, -0.0004, -0.1060,  0.2072, -0.2469, -0.2248,  0.1801,  0.1501,\n",
       "                       0.1775,  0.0874,  0.0816,  0.0956, -0.0806, -0.1911, -0.0579, -0.0858,\n",
       "                      -0.1161,  0.1634, -0.1723,  0.1799,  0.0916,  0.1141,  0.1951, -0.1066])),\n",
       "             ('dnn_network.1.weight',\n",
       "              tensor([[-0.0990,  0.0795, -0.0957,  ..., -0.0973, -0.1166,  0.0281],\n",
       "                      [ 0.0745,  0.0023,  0.0046,  ..., -0.0253, -0.0156, -0.1035],\n",
       "                      [-0.1110,  0.0248,  0.0810,  ...,  0.0493,  0.0979,  0.1002],\n",
       "                      ...,\n",
       "                      [-0.0190, -0.0228, -0.0458,  ..., -0.0990, -0.0888,  0.1216],\n",
       "                      [-0.1062,  0.1120,  0.0851,  ...,  0.0263,  0.1100,  0.0596],\n",
       "                      [-0.0025,  0.0912, -0.0383,  ...,  0.0803,  0.1189,  0.0444]])),\n",
       "             ('dnn_network.1.bias',\n",
       "              tensor([-0.1087, -0.0261, -0.1095, -0.0422,  0.0937, -0.0883,  0.0743,  0.0654,\n",
       "                       0.0289, -0.0150, -0.0333,  0.0584,  0.0366, -0.0757,  0.1228, -0.0812,\n",
       "                       0.0278,  0.0571,  0.0744,  0.0178,  0.0289, -0.0999,  0.0386, -0.0317,\n",
       "                       0.0337, -0.0641, -0.0676, -0.0274, -0.1013, -0.0802, -0.1162, -0.0738])),\n",
       "             ('dnn_network.2.weight',\n",
       "              tensor([[ 0.1300,  0.0899, -0.0424,  0.0568, -0.0308,  0.0312,  0.1345,  0.1304,\n",
       "                       -0.1283,  0.1573,  0.1163,  0.1246,  0.1413,  0.0829, -0.0646, -0.0567,\n",
       "                       -0.1181,  0.1272,  0.0937, -0.1293,  0.1324, -0.1761, -0.1052,  0.1698,\n",
       "                        0.0791, -0.0647, -0.0423,  0.0464,  0.1101,  0.1640, -0.0323,  0.0357],\n",
       "                      [-0.1548, -0.0427,  0.1440,  0.1418,  0.1242,  0.1265, -0.0991, -0.0855,\n",
       "                        0.1745,  0.0974, -0.1316, -0.1407, -0.1182,  0.0029, -0.0894, -0.0174,\n",
       "                        0.1323, -0.1402, -0.1383, -0.1401,  0.1151, -0.1103, -0.0388,  0.0518,\n",
       "                        0.1213,  0.0578,  0.1435,  0.0036, -0.0858, -0.1542,  0.0038,  0.0126],\n",
       "                      [-0.1279, -0.0839,  0.1316,  0.1639, -0.1069, -0.1396,  0.1512, -0.0949,\n",
       "                       -0.1055,  0.1559,  0.0636, -0.0898,  0.0834,  0.1299,  0.0540, -0.0529,\n",
       "                       -0.0797,  0.0426,  0.1621, -0.0855, -0.1085,  0.0648,  0.0377, -0.0950,\n",
       "                        0.0582, -0.0324,  0.1442, -0.1630, -0.1448, -0.1588, -0.0790,  0.0620],\n",
       "                      [ 0.0427, -0.1092,  0.1168, -0.0869, -0.0868, -0.0365, -0.0540, -0.0746,\n",
       "                        0.0569,  0.0909,  0.0249, -0.1652, -0.0667, -0.1120,  0.1672,  0.0116,\n",
       "                        0.1689,  0.0681,  0.0700,  0.0210,  0.1515,  0.1261, -0.0720, -0.1569,\n",
       "                       -0.1686, -0.1342,  0.1319,  0.0808,  0.0664, -0.1061,  0.1112,  0.1245],\n",
       "                      [-0.0116, -0.0362,  0.1728, -0.1334,  0.0421, -0.1122, -0.1080,  0.0827,\n",
       "                        0.0304,  0.1484,  0.1518,  0.1034,  0.1563, -0.0286,  0.1360, -0.0780,\n",
       "                       -0.0427,  0.0793, -0.0335,  0.1405,  0.0379,  0.0004, -0.0016,  0.0362,\n",
       "                        0.0796, -0.1493, -0.1503, -0.0365, -0.0169,  0.0364,  0.1151, -0.1171],\n",
       "                      [ 0.0541, -0.1375,  0.1503, -0.0833, -0.0675,  0.1180,  0.0303, -0.1405,\n",
       "                       -0.1646,  0.0622,  0.1643, -0.1724, -0.1165, -0.0830, -0.1201, -0.1460,\n",
       "                        0.0431,  0.0660,  0.0421,  0.0681,  0.0894, -0.0526, -0.0543, -0.0849,\n",
       "                       -0.0322, -0.1241,  0.0865,  0.0858,  0.1243, -0.0663,  0.1125,  0.1333],\n",
       "                      [-0.0915,  0.0833,  0.0999,  0.1068, -0.0131, -0.0740, -0.0769,  0.0645,\n",
       "                       -0.0728,  0.1671, -0.0400,  0.1587, -0.0967,  0.0612, -0.1192,  0.1013,\n",
       "                        0.0062,  0.0308,  0.1751, -0.0274, -0.0593,  0.0781, -0.0313, -0.1077,\n",
       "                        0.0323, -0.0684,  0.1482, -0.1525, -0.0673,  0.0446,  0.0385, -0.1288],\n",
       "                      [-0.0527,  0.1071,  0.1043,  0.0316, -0.1029,  0.0413, -0.1093,  0.0760,\n",
       "                       -0.0399, -0.1146,  0.0643, -0.0454, -0.0422,  0.0788,  0.1257, -0.1355,\n",
       "                        0.1114, -0.0453,  0.1252, -0.1250, -0.1494, -0.0329, -0.0089, -0.1450,\n",
       "                       -0.0840,  0.1404,  0.1153, -0.0379, -0.1446, -0.1093,  0.1764,  0.0245],\n",
       "                      [-0.1078, -0.1293,  0.0875,  0.0327, -0.0267, -0.0671,  0.0766,  0.1164,\n",
       "                       -0.1373,  0.0082,  0.0683,  0.0937,  0.0161,  0.0010,  0.1618,  0.1557,\n",
       "                        0.1309, -0.1669,  0.0137,  0.0652,  0.0309,  0.1116,  0.0438, -0.1596,\n",
       "                       -0.1067,  0.1132, -0.0995, -0.1318, -0.0992,  0.0413,  0.0753,  0.1182],\n",
       "                      [-0.0456,  0.0319, -0.1675,  0.0703,  0.1748, -0.1229,  0.1373, -0.1765,\n",
       "                        0.0189, -0.0643, -0.0119,  0.1738,  0.0850, -0.1052, -0.1736,  0.0259,\n",
       "                       -0.0570, -0.0123,  0.0848,  0.0192, -0.0567, -0.0396,  0.0656,  0.0585,\n",
       "                       -0.0369, -0.1656, -0.0319, -0.1160, -0.0762, -0.0909, -0.0217, -0.0589],\n",
       "                      [-0.1158, -0.0865, -0.0286,  0.0653, -0.0746, -0.0324,  0.0681, -0.1545,\n",
       "                        0.1701, -0.1576,  0.0051, -0.0560,  0.1345,  0.0705,  0.0354,  0.0127,\n",
       "                        0.1266,  0.0089, -0.1077, -0.1197, -0.1003,  0.1592,  0.0018, -0.1195,\n",
       "                       -0.1639,  0.1660,  0.0243, -0.0266,  0.0989, -0.1110, -0.0047,  0.0846],\n",
       "                      [-0.1292, -0.1449, -0.0808, -0.1539, -0.1207,  0.1481,  0.0068, -0.0618,\n",
       "                       -0.1722, -0.1672, -0.0736,  0.0935,  0.1111,  0.1424, -0.1086,  0.0436,\n",
       "                        0.0014, -0.1500, -0.1755,  0.1341,  0.1748,  0.0887, -0.0567, -0.1360,\n",
       "                        0.0692, -0.0194,  0.0517,  0.0447, -0.1656,  0.1282,  0.1553,  0.0973],\n",
       "                      [-0.0524,  0.1455, -0.0785, -0.1642,  0.0529,  0.1369, -0.1042, -0.1023,\n",
       "                       -0.0885,  0.1147, -0.0780, -0.1568, -0.1191,  0.0126, -0.1724, -0.0421,\n",
       "                        0.1737,  0.1132, -0.1382,  0.1037,  0.1188,  0.0343, -0.1509, -0.0797,\n",
       "                        0.0750,  0.0663,  0.0750,  0.1493,  0.0053,  0.0750,  0.0487, -0.1289],\n",
       "                      [-0.0783, -0.0415,  0.1689, -0.0316, -0.0811, -0.0335,  0.0917, -0.0544,\n",
       "                       -0.0605,  0.0437,  0.0320,  0.1113, -0.0709,  0.1204,  0.1078,  0.1125,\n",
       "                        0.1217,  0.0487, -0.1598, -0.0735, -0.0443, -0.1143,  0.0597, -0.0722,\n",
       "                        0.1124,  0.1675, -0.0091, -0.0633,  0.0886,  0.0872, -0.1346, -0.0039],\n",
       "                      [-0.0050, -0.0056, -0.1663,  0.1523,  0.0799, -0.0181,  0.0820, -0.0027,\n",
       "                        0.1122,  0.1623,  0.0478, -0.0195, -0.1432,  0.1565,  0.1153, -0.1088,\n",
       "                        0.0228, -0.0740,  0.1298, -0.1259, -0.0421,  0.1279,  0.0918,  0.1235,\n",
       "                       -0.0371, -0.0814, -0.1106,  0.0051, -0.0924,  0.1435, -0.1268, -0.0094],\n",
       "                      [-0.0442, -0.1030,  0.0976, -0.0698, -0.1270,  0.1509, -0.1083,  0.1335,\n",
       "                        0.0234, -0.0429,  0.1178,  0.0147, -0.0636, -0.0202,  0.0831,  0.1698,\n",
       "                        0.0234, -0.0660,  0.0042,  0.1551, -0.0013, -0.0118, -0.1757, -0.1711,\n",
       "                       -0.0890,  0.1006, -0.0706,  0.0968,  0.1046, -0.1410, -0.0083,  0.1593]])),\n",
       "             ('dnn_network.2.bias',\n",
       "              tensor([ 0.0619,  0.0918, -0.0473,  0.1245,  0.1543,  0.1695,  0.0771,  0.0120,\n",
       "                      -0.0610,  0.1652, -0.1245,  0.1320,  0.0364, -0.0426, -0.0553,  0.1521])),\n",
       "             ('linear.weight',\n",
       "              tensor([[-0.1959,  0.1101, -0.0505,  0.1351,  0.0071, -0.2161,  0.0428, -0.0438,\n",
       "                       -0.1461, -0.0510,  0.0076,  0.0810,  0.0691,  0.0465, -0.0924, -0.2010],\n",
       "                      [ 0.0031, -0.0235, -0.2369, -0.1398, -0.0676,  0.0101,  0.0867,  0.2227,\n",
       "                       -0.0370,  0.0638,  0.2261, -0.1193,  0.1833, -0.1832, -0.1811, -0.0157],\n",
       "                      [ 0.0620, -0.1796,  0.2485,  0.0471, -0.1951,  0.0709,  0.2297, -0.1861,\n",
       "                       -0.0549, -0.0087,  0.1507,  0.1785, -0.0207, -0.1235,  0.2035,  0.0715],\n",
       "                      [ 0.1917,  0.1836,  0.0753,  0.2479,  0.1660,  0.0745, -0.0601, -0.2500,\n",
       "                        0.1280, -0.0937, -0.1300,  0.1647, -0.0758,  0.1165, -0.0901,  0.2497],\n",
       "                      [-0.0525,  0.1481, -0.0251,  0.0788,  0.1222,  0.0094, -0.0815, -0.0930,\n",
       "                        0.2086, -0.0351, -0.0703, -0.0480, -0.1166,  0.2079,  0.2472,  0.0595],\n",
       "                      [ 0.1493,  0.2017,  0.2043,  0.1859, -0.1139,  0.2451,  0.0447, -0.1753,\n",
       "                        0.0480,  0.0052,  0.1732, -0.2001,  0.2412,  0.0939,  0.1235,  0.2449],\n",
       "                      [-0.1563,  0.2000, -0.0650, -0.0652, -0.0915,  0.1123,  0.0482, -0.2144,\n",
       "                        0.1981,  0.1544,  0.1573,  0.1674,  0.1677,  0.2026, -0.2411, -0.2416],\n",
       "                      [-0.1422,  0.0910, -0.0963,  0.1854,  0.1049,  0.1089, -0.2278,  0.0683,\n",
       "                        0.1335, -0.1057, -0.1886, -0.1814,  0.0575, -0.1289,  0.1955, -0.0016]])),\n",
       "             ('linear.bias',\n",
       "              tensor([ 0.0601, -0.1838,  0.1706, -0.1394, -0.0611, -0.0288,  0.1872, -0.1807])),\n",
       "             ('linear2.weight',\n",
       "              tensor([[-0.2243, -0.1967, -0.1198,  0.1907, -0.1828, -0.0936, -0.2042,  0.0292,\n",
       "                       -0.0934, -0.0588, -0.1052,  0.1138, -0.2149,  0.0086,  0.1363, -0.1850]])),\n",
       "             ('linear2.bias', tensor([-0.0011]))])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neural_mf.state_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:42:45.212823Z",
     "start_time": "2020-10-21T09:42:45.197863Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<All keys matched successfully>"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重新更新模型的参数\n",
    "neural_mf.load_state_dict(old_param)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T09:50:36.686554Z",
     "start_time": "2020-10-21T09:50:36.660621Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([('MF_Embedding_User.weight',\n",
       "              tensor([[-10.9049,   2.0719,   4.0300,  ...,  -3.8390, -14.3656,   2.9681],\n",
       "                      [ -0.7090,  -1.1438,   3.7491,  ...,  -6.2249,  -8.4540,   4.3511],\n",
       "                      [ -8.5488,  -4.4219,   4.3419,  ...,  -9.4483,  -4.8121,   2.1526],\n",
       "                      ...,\n",
       "                      [ -8.2142,  -2.5525,   3.6702,  ...,  -4.8879, -10.4885,   0.7474],\n",
       "                      [ -5.4971,   1.8283,   3.5808,  ...,  -8.7869, -18.5828,   3.7338],\n",
       "                      [ -2.2635,  -7.6829,   2.3591,  ...,  -1.6704, -12.4867,   1.1410]])),\n",
       "             ('MF_Embedding_Item.weight',\n",
       "              tensor([[  1.5389,  -3.9638,   1.7898,  ...,   1.7804,   6.2358,  -0.5696],\n",
       "                      [ -4.0041,   3.1085,  -0.4557,  ...,  10.5345,  -0.4570,   4.2654],\n",
       "                      [  4.9035,   5.8368,   1.4269,  ...,   4.1320,   5.8839,  -1.8388],\n",
       "                      ...,\n",
       "                      [ 24.8963,  21.7012, -18.6698,  ...,  37.9214, -30.0673,  17.4988],\n",
       "                      [ 24.3937,  22.5349, -17.3940,  ...,  40.3869, -28.5746,  16.3597],\n",
       "                      [ 25.3557,  21.3686, -17.5266,  ...,  37.7154, -28.8656,  17.3751]])),\n",
       "             ('MLP_Embedding_User.weight',\n",
       "              tensor([[ 4.9950, -6.9767,  5.3551,  ...,  5.7589, -7.3663, -5.6092],\n",
       "                      [ 4.8528, -4.5118,  5.2388,  ...,  3.0570, -8.8941, -4.2340],\n",
       "                      [ 7.3539, -8.0234,  7.4366,  ...,  8.1099, -6.6147, -1.7349],\n",
       "                      ...,\n",
       "                      [ 7.1080, -6.4246,  5.2171,  ...,  4.6690, -8.0232, -4.8722],\n",
       "                      [ 6.2267, -4.4847,  3.1904,  ...,  5.8430, -7.1987, -8.3548],\n",
       "                      [ 6.2948, -5.2223,  2.8955,  ...,  2.7126, -7.0047, -4.7766]])),\n",
       "             ('MLP_Embedding_Item.weight',\n",
       "              tensor([[ -7.6054,  -1.8106,   3.5323,  ...,   1.4687,  -0.0955,   0.1854],\n",
       "                      [  1.0725,  -0.7402,   3.8252,  ...,   2.2107,  -3.3219,   0.5598],\n",
       "                      [  1.3406,   0.8860,   1.6785,  ...,  -2.9676,  -0.1690,  -1.2765],\n",
       "                      ...,\n",
       "                      [ 34.6034,  37.2670,  72.4326,  ...,  76.0341,  21.0036,  15.3670],\n",
       "                      [ 66.4463,  23.0013, -20.5897,  ...,   4.5070, -62.4054, -13.1060],\n",
       "                      [ 22.3832,  29.0571, -39.4070,  ...,  76.2729, -40.6507,  33.2883]])),\n",
       "             ('dnn_network.0.weight',\n",
       "              tensor([[ 0.4368,  1.7973, -0.5419,  ...,  1.7270,  0.0888, -1.6420],\n",
       "                      [ 0.5062,  0.2336, -1.9969,  ..., -0.3831, -0.6298,  0.2523],\n",
       "                      [ 0.2225, -1.3596,  0.3727,  ..., -1.4107,  0.2095,  1.0465],\n",
       "                      ...,\n",
       "                      [ 0.3650, -0.6041,  0.5366,  ...,  0.4176, -0.0066, -0.2458],\n",
       "                      [ 0.0717, -0.9904, -0.9272,  ..., -1.2634,  1.7401, -1.4285],\n",
       "                      [ 0.7536, -1.2126, -0.3548,  ...,  0.3725, -0.0333, -0.5585]])),\n",
       "             ('dnn_network.0.bias',\n",
       "              tensor([-3.5007,  6.3196, -0.1677, -6.3300, -6.3476, -9.1748, -3.4646,  1.7153,\n",
       "                      -3.5787, -2.2585, -1.3778,  3.1100, -0.1065, -5.7601, -3.9592, -0.1380,\n",
       "                       0.7818,  1.7002,  0.3720,  4.9113,  1.5970, -4.0904,  0.8415, -2.9764,\n",
       "                      -1.4151, -3.6436, -7.1392, -2.2284, -0.9216, -3.3673, -1.6228, -4.1291,\n",
       "                      -6.1717, -1.1169, -3.1710, -0.5146, -6.4493, -0.0430, -0.4656, -3.5691,\n",
       "                      -5.5052, -0.6032, -2.1099, -0.9976, -1.7648,  0.4023, -5.9464, -7.1766,\n",
       "                      -0.4538, -4.6623,  1.5147,  1.1372, -2.6150,  0.3051, -1.7698, -4.1466,\n",
       "                      -2.3126, -6.5316, -1.1637, -0.3540, -3.3683, -8.9138,  2.2559, -2.9516])),\n",
       "             ('dnn_network.1.weight',\n",
       "              tensor([[-1.7730,  0.2150, -0.4904,  ..., -0.5599,  0.2608,  0.1584],\n",
       "                      [-0.3964,  0.8510, -0.1182,  ...,  0.8892,  0.6909,  0.2816],\n",
       "                      [-0.1679, -0.1734, -0.1613,  ..., -0.0971, -0.1580, -0.0425],\n",
       "                      ...,\n",
       "                      [-2.4966, -0.9750, -1.3330,  ...,  2.0053, -0.8791,  1.2811],\n",
       "                      [-0.0567, -0.3060, -0.2323,  ..., -0.0489, -0.0565, -0.1036],\n",
       "                      [ 0.0722,  0.3841, -0.0519,  ...,  0.6684,  0.6451, -0.9465]])),\n",
       "             ('dnn_network.1.bias',\n",
       "              tensor([-5.1443, -2.2667, -0.2815, -6.6957, -5.1257, -6.8520, -1.0225, -5.4206,\n",
       "                      -0.1870, -3.7691, -1.9530,  1.7917, -3.2754, -0.8683, -9.9854,  4.9422,\n",
       "                       1.2290,  6.5169,  1.1433, -2.1813,  1.7972, -2.7156, -3.9621, -3.4875,\n",
       "                      -3.9099,  0.3950, -0.6878, -4.0175,  4.7664,  0.5753, -0.2987, -4.2293])),\n",
       "             ('dnn_network.2.weight',\n",
       "              tensor([[-4.4051e-02, -8.1934e-01,  4.5098e-03, -7.2131e-02, -6.4582e-01,\n",
       "                        4.3078e-01,  4.3461e-01,  5.9002e-01, -3.1858e-03, -2.8823e-01,\n",
       "                       -3.8492e-02, -2.7279e-01, -5.8295e-01,  9.0764e-01, -2.1838e-01,\n",
       "                        2.4018e-01,  7.6393e-01,  8.8929e-01, -2.5254e-01, -2.8079e-01,\n",
       "                       -2.7408e-01, -9.3145e-01, -5.3054e-01, -6.0861e-01, -1.1109e-02,\n",
       "                       -4.1128e-01, -1.8452e-02, -9.3227e-02,  1.5636e-01,  7.8228e-01,\n",
       "                        8.2843e-02, -9.5167e-01],\n",
       "                      [-3.9500e-01, -7.9967e-01,  2.1564e-02, -6.1640e-01, -6.2433e-01,\n",
       "                        1.5546e+00,  1.1757e+00,  7.1575e-01, -2.3042e-02,  1.3887e+00,\n",
       "                       -5.7029e-01, -1.9144e-01, -5.1052e-01, -8.4609e-01, -7.6374e-01,\n",
       "                        1.1316e+00,  9.4472e-01, -3.4024e-03, -4.1610e-01, -2.5122e-01,\n",
       "                        6.8861e-01, -1.3120e-01, -1.5077e+00, -9.2809e-01, -5.0214e-01,\n",
       "                       -8.9124e-01, -2.3927e-01, -2.6621e-01,  1.0912e+00,  9.7414e-01,\n",
       "                       -8.5836e-02, -1.1813e+00],\n",
       "                      [ 9.1263e-01,  4.2344e-01, -4.6091e-02,  6.0063e-01,  6.1244e-01,\n",
       "                       -6.7338e-01, -1.3431e+00, -2.0414e-01, -4.9257e-02, -1.2823e+00,\n",
       "                        7.6319e-01,  4.4577e-02,  1.5136e-01,  1.1289e+00,  9.7425e-01,\n",
       "                       -1.0469e+00, -5.5425e-01,  4.4282e-01,  1.3582e-01,  8.3506e-01,\n",
       "                       -1.0269e+00,  5.6188e-01,  1.1865e+00,  1.0896e+00,  9.4982e-01,\n",
       "                        6.0999e-01, -7.5473e-02,  7.3114e-01, -1.1561e+00, -7.0813e-01,\n",
       "                       -6.0458e-02,  4.0971e-01],\n",
       "                      [-1.5414e-01, -2.9913e-01,  1.0456e-01, -6.1169e-01,  4.8296e-01,\n",
       "                        2.0147e+00, -1.8886e-01,  8.1915e-02,  1.3655e-01, -1.6800e-01,\n",
       "                       -6.6916e-01, -1.3672e+00, -1.0149e+00,  1.9609e+00, -2.0252e-01,\n",
       "                       -2.8055e-01,  9.1824e-01, -1.3270e+00, -4.5422e-01, -1.4322e+00,\n",
       "                        4.3314e-01, -6.6243e-01, -2.0089e-01,  2.8031e-01, -1.1099e-01,\n",
       "                       -2.0442e-02,  7.1619e-01,  7.5591e-01, -9.4984e-01,  1.5441e+00,\n",
       "                        1.3560e-01,  4.9008e-01],\n",
       "                      [-3.2995e-01,  3.7215e-01, -2.8340e-03,  6.8691e-02, -2.1678e-01,\n",
       "                       -1.8338e-01,  4.8511e-01,  1.0694e+00, -1.0057e-02,  1.1135e+00,\n",
       "                        1.7062e-01, -6.1651e-02, -4.5095e-01,  1.1148e+00, -5.9234e-01,\n",
       "                       -5.2451e-01, -2.9269e-01, -1.5681e+00, -3.0650e-01, -5.1542e-01,\n",
       "                       -3.5460e-01, -5.2901e-02,  5.7919e-02, -1.0059e-02, -6.5175e-02,\n",
       "                       -2.1371e-01, -2.5414e-01, -2.8552e-01, -3.4066e-01,  1.6789e+00,\n",
       "                       -2.9424e-02,  3.3820e-01],\n",
       "                      [ 1.3778e+00, -1.6648e+00, -3.3236e-02, -4.6325e-01, -4.1322e-01,\n",
       "                        5.9705e-02,  2.4918e-01, -8.3488e-01,  1.8945e-03, -3.6855e-01,\n",
       "                        9.3135e-01, -9.1142e-01,  8.5903e-01,  3.3044e+00, -1.4469e+00,\n",
       "                       -1.1948e+00, -3.2011e-02, -2.9612e-01, -3.4531e-01,  6.5727e-01,\n",
       "                       -6.9806e-01,  1.4933e+00,  1.9093e+00,  6.3497e-01,  4.3956e-01,\n",
       "                        5.8230e-01, -2.1951e-04,  4.9304e-01, -4.9308e-01, -5.4735e-01,\n",
       "                       -2.1270e-01,  1.3244e+00],\n",
       "                      [-2.6208e-01, -1.2058e+00,  8.1391e-02, -2.0514e+00,  6.4715e-02,\n",
       "                        1.0197e+00, -1.9273e-01,  6.3296e-01, -8.9678e-02,  9.1543e-01,\n",
       "                       -1.2285e-01, -1.0416e+00, -1.2093e+00,  1.8762e+00,  5.0103e-01,\n",
       "                        2.0818e-01, -8.0986e-01, -4.3927e-01, -1.8285e+00, -6.5149e-01,\n",
       "                       -1.2800e-01,  9.6707e-01, -6.9060e-01,  6.0702e-03,  1.5932e+00,\n",
       "                       -9.5766e-01,  4.3451e-01,  1.5812e+00,  5.8654e-01,  4.0028e-01,\n",
       "                       -1.7357e-01,  2.1995e+00],\n",
       "                      [ 3.3405e-01, -1.1557e+00, -9.7402e-02, -5.0923e-02, -1.4550e-01,\n",
       "                        7.7199e-01,  6.5861e-01,  7.2618e-01, -9.5417e-02, -7.7634e-01,\n",
       "                       -5.8917e-02, -9.5223e-01, -7.3283e-01,  6.2620e-01, -1.0915e+00,\n",
       "                        9.3632e-01,  5.7244e-01, -5.2406e-01, -7.7885e-01,  9.2394e-02,\n",
       "                       -1.1682e+00,  3.2472e-01,  4.4773e-01,  1.5735e-01,  6.7605e-01,\n",
       "                       -7.1062e-02, -2.2116e-02,  8.8030e-01, -1.3553e-01,  1.2833e+00,\n",
       "                       -2.4388e-01, -4.4657e-01],\n",
       "                      [-1.6452e-01,  1.0688e+00,  9.4670e-02,  2.5441e-01,  9.1669e-02,\n",
       "                        3.8534e-01, -2.4009e+00,  6.3380e-01,  1.6183e-02, -4.8297e-01,\n",
       "                       -1.1561e+00, -1.4645e+00,  1.2467e+00,  1.0534e+00,  5.4793e-01,\n",
       "                       -4.0388e-01,  3.4412e-01, -1.6244e+00, -1.0683e+00, -2.1446e-01,\n",
       "                       -9.0056e-01,  8.3586e-01,  7.5266e-01, -8.8870e-01,  1.2376e+00,\n",
       "                        5.4283e-01,  4.2756e-01,  1.7335e+00, -3.8808e-01, -9.2259e-03,\n",
       "                        1.1269e-01,  1.5592e+00],\n",
       "                      [-4.4713e-02, -6.4907e-02,  2.4254e-02, -1.2601e-01, -4.4313e-01,\n",
       "                       -6.4218e-02, -2.6300e-01, -8.1047e-02, -1.5160e-01, -6.9007e-02,\n",
       "                       -3.2775e-02, -2.8450e-01, -5.7191e-01,  9.8572e-02,  9.3093e-02,\n",
       "                       -2.5621e-01, -5.1281e-02, -3.9777e-01, -2.0278e-01, -6.2740e-01,\n",
       "                       -2.1534e-01, -2.2273e-01, -7.7451e-03, -6.5237e-01, -5.0229e-02,\n",
       "                       -3.0370e-01, -1.1697e-01, -2.2330e-01, -4.8089e-01, -1.0627e-01,\n",
       "                       -9.4544e-02,  2.9706e-01],\n",
       "                      [ 1.3601e+00,  1.3384e+00,  9.4160e-02, -1.7131e-01,  5.5850e-01,\n",
       "                       -1.8411e-02,  3.8783e-01,  1.2751e+00, -4.6852e-02,  2.9104e-01,\n",
       "                        2.3390e-01, -2.7177e-01,  2.2739e-01,  6.6171e-01,  2.0763e-02,\n",
       "                       -1.7252e-01, -4.1478e-01, -3.2555e-01, -6.2022e-01, -7.8405e-01,\n",
       "                       -1.7572e+00, -7.6649e-01, -1.1945e+00, -3.1329e-01, -2.7036e-01,\n",
       "                        2.3988e-01,  2.8656e-01,  4.3302e-01, -6.6478e-01, -3.2320e-01,\n",
       "                       -6.6820e-02,  1.8977e+00],\n",
       "                      [ 4.5706e-02,  1.9536e-01, -1.7208e-01,  8.0733e-01, -5.3704e-02,\n",
       "                        6.0554e-01, -3.0538e-01,  1.9226e-02,  7.2696e-02, -6.4441e-01,\n",
       "                        8.3836e-01, -6.0982e-01, -2.6637e-01,  1.7378e+00, -3.2646e-01,\n",
       "                       -1.0202e+00, -1.0150e+00, -1.1867e-01, -8.1304e-01,  1.2470e+00,\n",
       "                       -4.4726e-01, -1.7653e-01, -3.7283e-01, -5.6710e-01,  1.0258e+00,\n",
       "                       -3.9244e-01,  1.0244e-01,  8.6848e-01, -8.0510e-01, -6.4596e-01,\n",
       "                       -2.3615e-01,  6.3139e-02],\n",
       "                      [ 2.2190e-01,  1.9679e+00, -1.3971e-01, -8.3897e-01,  4.1391e-02,\n",
       "                       -1.5867e-01, -2.4236e+00,  9.6721e-02, -1.4513e-01, -1.1553e+00,\n",
       "                       -1.3676e+00, -8.5156e-01, -2.6498e-02,  2.9308e+00,  1.8696e+00,\n",
       "                       -5.5079e-01, -2.8262e+00, -2.8420e+00, -9.4313e-01,  4.2039e-01,\n",
       "                        2.7546e-01,  9.5783e-01,  1.2222e-01, -2.2993e-01,  1.4012e+00,\n",
       "                       -1.7311e+00, -2.1596e-01,  4.7301e+00, -9.6120e-01,  1.0940e+00,\n",
       "                        1.1745e-01,  3.8736e+00],\n",
       "                      [-2.4444e-01, -7.1277e+00, -8.1993e-02, -1.1204e+00,  1.4396e+00,\n",
       "                       -2.3764e-01,  2.1808e+00, -3.4685e+00, -1.0268e-01,  6.4083e-01,\n",
       "                       -7.6345e-01, -6.0523e-01,  3.0098e-01,  8.5429e-01,  4.9566e-01,\n",
       "                       -8.9938e-01,  5.0635e-01, -1.6473e+00, -7.4683e-01, -2.1837e-01,\n",
       "                        1.5698e-01,  9.9786e-01, -2.1973e+00,  3.1748e-01, -3.3872e+00,\n",
       "                       -2.5172e-02,  3.0819e-01, -2.1796e+00, -1.2126e+00, -4.1022e-01,\n",
       "                       -1.5058e-01,  6.7523e-01],\n",
       "                      [ 3.3748e-01,  4.0115e-01, -1.5425e-01,  5.1557e-02,  7.1352e-02,\n",
       "                        3.2070e-01, -1.9699e-01, -8.4321e-03, -1.0195e-01, -5.1266e-01,\n",
       "                       -1.7445e-01, -6.6549e-01, -2.8901e-01,  1.4721e+00,  4.2606e-01,\n",
       "                       -8.4854e-01, -2.1581e-01, -1.3488e+00, -1.0471e+00, -1.9348e-01,\n",
       "                       -4.9957e-01,  8.8806e-01,  1.1979e+00, -3.2216e-02,  1.7165e+00,\n",
       "                       -1.0433e+00,  2.3239e-01,  1.3549e+00, -1.1514e+00,  3.6782e-01,\n",
       "                        5.3545e-02,  2.1251e+00],\n",
       "                      [ 2.7569e-01, -1.7855e+00,  2.5942e-02, -5.4490e-01, -1.4425e+00,\n",
       "                        6.1914e-01,  4.0906e-01,  1.1861e+00, -9.2218e-02, -5.4406e-02,\n",
       "                       -1.0434e+00, -5.2320e-01, -1.7078e+00, -3.1370e-01, -2.9468e-01,\n",
       "                       -4.9601e-01,  3.9774e-01,  1.0367e+00, -1.2951e+00, -5.6330e-01,\n",
       "                       -4.8770e-01, -7.0061e-01, -4.7665e-01, -3.5500e-02, -1.3213e+00,\n",
       "                       -6.0825e-01, -3.6014e-01,  1.1299e+00,  7.2811e-02, -9.8238e-02,\n",
       "                        1.8612e-02,  3.0124e-01]])),\n",
       "             ('dnn_network.2.bias',\n",
       "              tensor([11.5392,  3.1231, -1.4879, -6.0367, -4.2835, -0.6179,  0.5533, -1.4626,\n",
       "                      -5.2971, -0.9771, -6.9666, -5.0035, -5.3833, -4.3558, -2.7910,  0.9846])),\n",
       "             ('linear.weight',\n",
       "              tensor([[-0.1959,  0.1101, -0.0505,  0.1351,  0.0071, -0.2161,  0.0428, -0.0438,\n",
       "                       -0.1461, -0.0510,  0.0076,  0.0810,  0.0691,  0.0465, -0.0924, -0.2010],\n",
       "                      [ 0.0031, -0.0235, -0.2369, -0.1398, -0.0676,  0.0101,  0.0867,  0.2227,\n",
       "                       -0.0370,  0.0638,  0.2261, -0.1193,  0.1833, -0.1832, -0.1811, -0.0157],\n",
       "                      [ 0.0620, -0.1796,  0.2485,  0.0471, -0.1951,  0.0709,  0.2297, -0.1861,\n",
       "                       -0.0549, -0.0087,  0.1507,  0.1785, -0.0207, -0.1235,  0.2035,  0.0715],\n",
       "                      [ 0.1917,  0.1836,  0.0753,  0.2479,  0.1660,  0.0745, -0.0601, -0.2500,\n",
       "                        0.1280, -0.0937, -0.1300,  0.1647, -0.0758,  0.1165, -0.0901,  0.2497],\n",
       "                      [-0.0525,  0.1481, -0.0251,  0.0788,  0.1222,  0.0094, -0.0815, -0.0930,\n",
       "                        0.2086, -0.0351, -0.0703, -0.0480, -0.1166,  0.2079,  0.2472,  0.0595],\n",
       "                      [ 0.1493,  0.2017,  0.2043,  0.1859, -0.1139,  0.2451,  0.0447, -0.1753,\n",
       "                        0.0480,  0.0052,  0.1732, -0.2001,  0.2412,  0.0939,  0.1235,  0.2449],\n",
       "                      [-0.1563,  0.2000, -0.0650, -0.0652, -0.0915,  0.1123,  0.0482, -0.2144,\n",
       "                        0.1981,  0.1544,  0.1573,  0.1674,  0.1677,  0.2026, -0.2411, -0.2416],\n",
       "                      [-0.1422,  0.0910, -0.0963,  0.1854,  0.1049,  0.1089, -0.2278,  0.0683,\n",
       "                        0.1335, -0.1057, -0.1886, -0.1814,  0.0575, -0.1289,  0.1955, -0.0016]])),\n",
       "             ('linear.bias',\n",
       "              tensor([ 0.0601, -0.1838,  0.1706, -0.1394, -0.0611, -0.0288,  0.1872, -0.1807])),\n",
       "             ('linear2.weight',\n",
       "              tensor([[-0.2243, -0.1967, -0.1198,  0.1907, -0.1828, -0.0936, -0.2042,  0.0292,\n",
       "                       -0.0934, -0.0588, -0.1052,  0.1138, -0.2149,  0.0086,  0.1363, -0.1850]])),\n",
       "             ('linear2.bias', tensor([-0.0011]))])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 更新完毕\n",
    "neural_mf.state_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T12:20:17.283961Z",
     "start_time": "2020-10-21T12:20:16.782304Z"
    }
   },
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "Expected object of backend CPU but got backend CUDA for argument #3 'index'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-121-1241c94a4445>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     19\u001b[0m         \u001b[1;31m# 正向传播\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m         \u001b[0mpredictions\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mneural_mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfeatures\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     21\u001b[0m         \u001b[0mloss\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mloss_func\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpredictions\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlabels\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     22\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\envs\\pytorch_gpu\\lib\\site-packages\\torch\\nn\\modules\\module.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *input, **kwargs)\u001b[0m\n\u001b[0;32m    545\u001b[0m             \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    546\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 547\u001b[1;33m             \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    548\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mhook\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_forward_hooks\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    549\u001b[0m             \u001b[0mhook_result\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mhook\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-13-ff288e5dd44d>\u001b[0m in \u001b[0;36mforward\u001b[1;34m(self, inputs)\u001b[0m\n\u001b[0;32m     25\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     26\u001b[0m         \u001b[1;31m# MF模型的计算 用户和物品的embedding\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 27\u001b[1;33m         \u001b[0mMF_Embedding_User\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mMF_Embedding_User\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 这里踩了个坑， 千万不要写成[0]， 我们这里是第一列\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     28\u001b[0m         \u001b[0mMF_Embedding_Item\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mMF_Embedding_Item\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     29\u001b[0m         \u001b[1;31m# 两个向量点积过一个全连接\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\envs\\pytorch_gpu\\lib\\site-packages\\torch\\nn\\modules\\module.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *input, **kwargs)\u001b[0m\n\u001b[0;32m    545\u001b[0m             \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    546\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 547\u001b[1;33m             \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    548\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mhook\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_forward_hooks\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    549\u001b[0m             \u001b[0mhook_result\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mhook\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\envs\\pytorch_gpu\\lib\\site-packages\\torch\\nn\\modules\\sparse.py\u001b[0m in \u001b[0;36mforward\u001b[1;34m(self, input)\u001b[0m\n\u001b[0;32m    112\u001b[0m         return F.embedding(\n\u001b[0;32m    113\u001b[0m             \u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mweight\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpadding_idx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmax_norm\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 114\u001b[1;33m             self.norm_type, self.scale_grad_by_freq, self.sparse)\n\u001b[0m\u001b[0;32m    115\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    116\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mextra_repr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\envs\\pytorch_gpu\\lib\\site-packages\\torch\\nn\\functional.py\u001b[0m in \u001b[0;36membedding\u001b[1;34m(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)\u001b[0m\n\u001b[0;32m   1465\u001b[0m         \u001b[1;31m# remove once script supports set_grad_enabled\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1466\u001b[0m         \u001b[0m_no_grad_embedding_renorm_\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mweight\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmax_norm\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnorm_type\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1467\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0membedding\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mweight\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpadding_idx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscale_grad_by_freq\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msparse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1468\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1469\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: Expected object of backend CPU but got backend CUDA for argument #3 'index'"
     ]
    }
   ],
   "source": [
    "# 再来训练一遍\n",
    "# 模型训练 \n",
    "best_hr, best_ndcg, best_iter = hr, ndcg, -1\n",
    "\n",
    "epochs = 10\n",
    "log_step_freq = 10000\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    \n",
    "    # 训练阶段\n",
    "    neural_mf.train()\n",
    "    loss_sum = 0.0\n",
    "    for step, (features, labels) in enumerate(dl_train, 1):\n",
    "        \n",
    "        features, labels = features.cuda(), labels.cuda()\n",
    "        # 梯度清零\n",
    "        optimizer.zero_grad()\n",
    "        \n",
    "        # 正向传播\n",
    "        predictions = neural_mf(features)\n",
    "        loss = loss_func(predictions, labels)\n",
    "        \n",
    "        # 反向传播求梯度\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        \n",
    "        # 打印batch级别日志\n",
    "        loss_sum += loss.item()\n",
    "        if step % log_step_freq == 0:\n",
    "            print((\"[step = %d] loss: %.3f\") %\n",
    "                  (step, loss_sum/step))\n",
    "    \n",
    "    # 验证阶段\n",
    "    neural_mf.eval()\n",
    "    (hits, ndcgs) = evaluate_model(neural_mf, testRatings, testNegatives, topK)\n",
    "    hr, ndcg = np.array(hits).mean(), np.array(ndcgs).mean()\n",
    "    if hr > best_hr:\n",
    "        best_hr, best_ndcg, best_iter = hr, ndcg, epoch\n",
    "        torch.save(neural_mf.state_dict(), 'Pre_train/m1-1m_NeuralMF.pkl')  \n",
    "        \n",
    "    info = (epoch, loss_sum/step, hr, ndcg)\n",
    "    print((\"\\nEPOCH = %d, loss = %.3f, hr = %.3f, ndcg = %.3f\") %info)\n",
    "print('Finished Training...') "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
